I have to work using binary formated numbers and I'm wondering if there's a simple and easy built in way to use them. I am aware of the bytearray
but it works with byte type and it is absolutely not intuitive (at least for me).
So, is there any way of handling binary numbers (assign them to a variable, perform bit operations, transform to ASCII, etc.) easily? If not built in, at least a nice and understandable module.
Just in case what I'm asking is not clear enough, here is an example of what I picture a nice way for handling binary would be:
bin_num1 = Binary('100')
bin_num2 = Binary(0)
print(bin_num1.and(bin_num2)) # Prints 000
I'm using Python 3.6 but any solution in any version would do the job.
Edit 1:
As pointed out in the comments, 0bXXX
could work to get a type int
from a binary just as bin()
would do the opossite. Still, this would be working with integers, the result returned would be an integer and character conversion (e.g. bin('a')
) would be an error and require further conversions (from str to int and then to binary).