Binary representation python – Python Program to Print Binary Representation of a Number

Binary Representation:

Get binary representation python: Binary (or base-2) is a numeral system with only two digits — 0 and 1. Computers use binary to store data and execute calculations, which means they only use zeros and ones. In Boolean logic, a single binary digit can only represent True (1) or False (0). Any integer, in fact, can be represented in binary.

Given a decimal number the task is to convert the given decimal to binary

Examples:

Example1:

Input:

given number =200

Output:

The binary representation of the given number 200  : 
11001000

Example2:

Input:

given number =1

Output:

The binary representation of the given number 1: 
1

Example3:

Input:

given number =32

Output:

The binary representation of the given number 32  : 
100000

Python Program to Print Binary Representation of a Number

Binary representation python: There are several ways to print the binary representation of a given decimal number some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1:Using recursive function

Binary representation python: To find a binary equivalent, divide the decimal number recursively by the value 2 until the decimal number hits zero. The remaining must be noted after each division. The binary equivalent of the decimal number is obtained by reversing the remaining values.

Algorithm:

  1. Get the given decimal number from the user or static input.
  2. If the input is larger than zero, divide it by 2 and record the remainder.
  3. Step 2 should be repeated until the decimal number reaches zero.
  4. The residual values should be printed.
  5. End of program

Below is the implementation:

def decitoBin(numb):
    # checking if the given number is greater than 1
    if numb > 1:
        # if it is greater than 1 then use recursive approach by dividing number by 2
        decitoBin(numb // 2)
    # printing the binary representation of the given number
    print(numb % 2, end='')


# Driver code
given_numb = 200
# passing given number to decitoBin function to print binary representation of the givennumb
print("The binary representation of the given number", given_numb, " : ")
decitoBin(given_numb)

Output:

The binary representation of the given number 200  : 
11001000

Method #2:Using while loop

Approach:

  • First we take a empty string say binstr.
  • We use while loop .
  • We will iterate till the number is greater than 0 (Condition of while statement)
  • We will get the last check bit whether it is set bit or not using % operator
  • Convert the set bit to string using str() function
  • Concatenate this bit(can be 1 or 0 ) to the binstr.
  • Reverse this binstr using slicing
  • Print the binary representation of the given number that is print binstr.

Below is the implementation:

def decitoBin(numb):
    # checking if the given number is greater than 1
    if numb > 1:
      # taking a empty string
        binastring = ""
        # looping till number greater than 0 using while loop
        while(numb > 0):
            # We will get the last check bit whether it is set bit or not using % operator
            checkbit = numb % 2
            # converting this checkbit to string using str() function
            checkbit = str(checkbit)
            # Concatenate this bit(can be 1 or 0 ) to the binstr.
            binastring = binastring+checkbit
            # divide the number by 2
            numb = numb//2
    # reverse the binary string
    binastring = binastring[::-1]
    # return the resultant binary string
    return binastring


# Driver code
given_numb = 200
# passing given number to decitoBin function to print binary representation of the givennumb
print("The binary representation of the given number", given_numb, " : ")
print(decitoBin(given_numb))

Output:

The binary representation of the given number 200  : 
11001000

Method #3:Using Built in Python function bin()

Approach:

  • We will use bin() function to convert the given decimal number to binary representation.
  • It will return the result in the form of  0bXXXXXXXXX where XXXXXXXX is binary representation.
  • To remove that 0b characters from the string we use two methods
  • Print the resultant binary representation.

i)Using replace function

We will replace the 0b in the binary string with empty string.

Below is the implementation:

def decitoBin(numb):
  # converting it to binary representation using bin() function
    binNumb = bin(numb)
    # replacing '0b' using replace function and replacing it with empty string
    binNumb = binNumb.replace('0b', '')
  # return the binary representation of the given number
    return binNumb


# Driver code
given_numb = 200
# passing given number to decitoBin function to print binary representation of the givennumb
print("The binary representation of the given number", given_numb, " : ")
print(decitoBin(given_numb))

Output:

The binary representation of the given number 200  : 
11001000

The binary representation of the given decimal number will be printed.

ii)Using slicing

We will slice from 2 index to last index of the result returned by binary string

Below is the implementation:

def decitoBin(numb):
  # converting it to binary representation using bin() function
    binNumb = bin(numb)
    # We will slice from 2 index to last index of the result returned by binary string
    binNumb = binNumb[2:]
  # return the binary representation of the given number
    return binNumb


# Driver code
given_numb = 200
# passing given number to decitoBin function to print binary representation of the givennumb
print("The binary representation of the given number", given_numb, " : ")
print(decitoBin(given_numb))

Output:

The binary representation of the given number 200  : 
11001000

The binary representation of the given decimal number will be printed.

Related Programs: