First consider a function name bin which take one parameter.
The parameter is the decimal number and then it print out the result. Like this
def bin(n):
print ("{:09b}".format(n)) # It work's for python 3x
Here {:09b} mean make the output binary which must have 9 digits. So as a result, if the output is 10 then for this -> {:09b} the output will be 000000010. If you put only {:b} then the result will be just accurate what we want.
Let me show a demo code..... In python Shell
>>> def bin(n): #created function print ("{:09b}".format(n))
>>> x = 0x98 # Give the value 98 in x >>> y = 0x64 >>> x , y # Show x , y respectively (152, 100) >>> x 152 >>> y 100 >>> bin(x) # parse x as parameter into bin function. 010011000 # bin function print this. Here the number is 8 digits but for {:09b} it put a extra 0 in beginning. >>> bin(y) 001100100
You can do any mathematical expression through it.
Just like this
>>> bin ( x - y ) #Subtraction of x , y 000110100 >>> bin (x * y) # Multiplication of x , y 11101101100000
0 Comments