Why does Python do this:
100|111 = 111
1000|1001 = 1001
Above two examples look like bitwise OR operation
But for the below example:
1000|1111 = 2047
Why is it 2047 and not 1111?
Why does Python do this:
100|111 = 111
1000|1001 = 1001
Above two examples look like bitwise OR operation
But for the below example:
1000|1111 = 2047
Why is it 2047 and not 1111?
It looks like you're performing the operation on integers, and hence on they're corresponding binary representation. You need to convert them to binary using the base argument in int, and then perform the bitwise OR:
bin(int('1000', 2) | int('1111', 2))
# '0b1111'
Otherwise you'd get:
1000|1111
# 2047
Which corresponds to the bitwise OR between the following binary numbers (not the actual 1s and 0s of the integer number):
bin(1000)
#'0b1111101000'
bin(1111)
# '0b10001010111'
int(bin(1000), 2) | int(bin(1111), 2)
# 2047