You can convert between a string representation of the binary using bin() and int()
>>> bin(88)
'0b1011000'
>>> int('0b1011000', 2)
88
>>>
>>> a=int('01100000', 2)
>>> b=int('00100110', 2)
>>> bin(a & b)
'0b100000'
>>> bin(a | b)
'0b1100110'
>>> bin(a ^ b)
'0b1000110'
Answer from John La Rooy on Stack OverflowYou can convert between a string representation of the binary using bin() and int()
>>> bin(88)
'0b1011000'
>>> int('0b1011000', 2)
88
>>>
>>> a=int('01100000', 2)
>>> b=int('00100110', 2)
>>> bin(a & b)
'0b100000'
>>> bin(a | b)
'0b1100110'
>>> bin(a ^ b)
'0b1000110'
I think you're confused about what binary is. Binary and decimal are just different representations of a number - e.g. 101 base 2 and 5 base 10 are the same number. The operations add, subtract, and compare operate on numbers - 101 base 2 == 5 base 10 and addition is the same logical operation no matter what base you're working in. The fact that your python interpreter may store things as binary internally doesn't affect how you work with it - if you have an integer type, just use +, -, etc.
If you have strings of binary digits, you'll have to either write your own implementation or convert them using the int(binaryString, 2) function.
Is there a way to see what the "real" binary number is when using bitwise operators like ~
Why use binary in python?
How to generate a binary file with 32bit numbers in Python.
Why numbers in binary representation has the prefix "0b" or "0B"?
B for binary
It's been that way for almost every programming language since at least C.
More on reddit.comSo to explain what I mean, in Python3 negative binary numbers are represented as something like -0b110 (decimal -6). So -0b110 is what Python shows me if I do: print(bin(~5))
But if I'm understanding things correctly, the "real" binary number of a ~ operation is not this, it's the inversion of the original binary number. So instead of getting -0b110 (decimal -6) from a binary of 0101 (decimal 5), I would like to see the inverted binary, which I believe is 1010.
The reason for this is that, as I try to get gud with these operators, it appears to me that Python is sometimes using the "real" binary number to perform operations and not the ones printed out by bin(). Now maybe I'm just confused, I find this pretty difficult but if I could print the "real" binary value it would make it easy for me to clarify things by observing what a script is doing in a more direct way.
I guess I could just write something that inverts the provided original binary, but I'm wondering if there's a direct way to have it print the output besides writing a function.