Read bit by bit from a 128 bit number - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Read bit by bit from a 128 bit number (/thread-17560.html) |
Read bit by bit from a 128 bit number - vdhingra - Apr-15-2019 I am trying to read bit by bit from a 128 bit binary number and want to use it. I was trying my logic on 8 bit number but not getting the bits in order def get_bit(value,n): return ((value >> n & 1)) for i in range (8): if (get_bit (10100010,i)): print"TRUE" else: print "FALSE"Output: 1 0 1 0 1 0 1 0 0 RE: Read bit by bit from a 128 bit number - scidam - Apr-16-2019 You've passed a very big number to your function: 10100010 wasn't treated as you expected, but as ten millions etc... Use get_bit(0b10100010,i) instead.
RE: Read bit by bit from a 128 bit number - vdhingra - Apr-16-2019 Thanks scidam for the response. Yes you are right if i add "0b" before the number then it works perfect. but my number "10100010" passed to "get_bit" is of type "str" which is causing problem. Any suggestions for that. RE: Read bit by bit from a 128 bit number - ichabod801 - Apr-16-2019 If x is a binary number as a string int(x, 2) will return an integer with the correct value.>>> b = '1101' >>> int(b) 1101 >>> int(b, 2) 13 |