Posted on by Kalkicode
Code Conversion

Convert Binary to Octal number

The problem is to convert a binary number into its equivalent octal representation. A binary number is a number expressed in the base-2 numeral system, which uses only two symbols, typically 0 and 1. On the other hand, an octal number is a number expressed in the base-8 numeral system, which uses eight symbols, 0 to 7.

Explanation using Example

Let's say we have the binary number 1011010.

We first group the digits into sets of three, starting from the rightmost digit:

1 011 010

Then, we can replace each set of three binary digits with its equivalent octal digit:

1 3 2

So the octal equivalent of the binary number 1011010 is 132.

Let's consider the test cases from the provided code:

  1. Test Case 1: Binary = "1111" After converting the binary number "1111" to its equivalent decimal number, we get 15. Then, we convert the decimal number 15 to its octal representation, which is "17".

  2. Test Case 2: Binary = "10111" After converting the binary number "10111" to its equivalent decimal number, we get 23. Then, we convert the decimal number 23 to its octal representation, which is "27".

  3. Test Case 3: Binary = "101011" After converting the binary number "101011" to its equivalent decimal number, we get 43. Then, we convert the decimal number 43 to its octal representation, which is "53".

  4. Test Case 4: Binary = "11011" After converting the binary number "11011" to its equivalent decimal number, we get 27. Then, we convert the decimal number 27 to its octal representation, which is "33".

  5. Test Case 5: Binary = "-1000110" The provided binary number "-1000110" is a negative binary number represented using two's complement. The algorithm first converts the binary number to its equivalent decimal representation, which is -70. Then, it converts the decimal number -70 to its octal representation, which is "-106".

Pseudocode

octal(number)
    result = 0
    multiplier = 1
    while number is not 0
        remainder = number % 8
        result = (remainder * multiplier) + result
        multiplier *= 10
        number = number / 8
    return result

binaryToOctal(num)
    if num length is 0
        return
    flag = false
    decimalNo = 0
    counter = 0
    index = num length - 1
    while index is greater than or equal to 0
        if num[index] is '1'
            decimalNo += (1 << counter)
        else if num[index] is not '0'
            if index is 0 and num[index] is '-'
                flag = true
            else
                return
        counter += 1
        index -= 1
    output = octal(decimalNo)
    if flag is true
        output = -output
    print "Binary : num"
    print "Octal : output"

Algorithm Explanation

The octal function is used to convert a decimal number into its equivalent octal representation. It performs the conversion using a while loop that iteratively divides the number by 8 and appends the remainder to the result in reverse order. The result is then returned as the octal representation.

The binaryToOctal function is used to convert a given binary number into its equivalent octal representation using the following steps:

  1. If the length of the num is 0, it means there are no binary digits to convert, and the function returns without doing anything.
  2. The algorithm initializes variables flag to indicate if the number is negative, decimalNo to store the equivalent decimal number, counter to keep track of the power of 2, and index to traverse the binary number from right to left.
  3. The algorithm iterates through the binary number from right to left. For each binary digit, if it is '1', the algorithm adds 2^counter to the decimalNo, where counter starts from 0 and increments for each digit.
  4. If the binary digit is not '0' or '1', it means the binary number is invalid, and the function returns without converting further.
  5. If the binary number is negative (indicated by a '-' sign at the beginning), the flag is set to true.
  6. After converting the binary number to its equivalent decimal number, the algorithm calls the octal function to convert the decimal number into its octal representation.
  7. If the original binary number was negative (flag is true), the result is negated before printing.
  8. Finally, the original binary number and its octal representation are printed.

Program List

Time Complexity

The time complexity of the provided algorithm depends on the number of binary digits in the given input. The conversion from binary to decimal takes linear time, and the conversion from decimal to octal also takes linear time. Therefore, the overall time complexity is O(n), where n is the number of binary digits in the input.

Resultant Output Explanation

The code correctly converts binary numbers to their equivalent octal representations for the given test cases:

  1. For binary number "1111", the equivalent octal representation is "17".
  2. For binary number "10111", the equivalent octal representation is "27".
  3. For binary number "101011", the equivalent octal representation is "53".
  4. For binary number "11011", the equivalent octal representation is "33".
  5. For binary number "-1000110", the equivalent octal representation is "-106".

The algorithm efficiently performs the conversion from binary to octal and provides the correct results for the provided test cases.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment