I tried to copy the example but I was wrong and I have no idea what to do.

a = 0b10111011
print a|bin(0b10111011)


You need a bitmask like 0b100 and then print bin(a | bitmask).

0b100 is the bitmask with the third bit on and only the third bit


Try this...

a = 0b10111011
mask = 0b100
print bin(a | mask)


Why the last one worked? Could you explain why the mask is only 3 binary digits?


a = 0b10111011
desired = a >> 3
print bin(a | desired)


Because the exercise asked you to:

Use a bitmask and the value a in order to achieve a result where the third bit from the right of a is turned on.

We want to leave the rest of value A unchanged, while only flipping the third bit. This is done by applying the specified mask through and OR operator. All other bits in the mask can be zero, because we want to leave all the other bits in A unchanged. And 0b00000100 is the exact same as 0b100 :smile:


a = 0b10111011
turn_it_on= a | mask
if bin((turn_it_on >> 3) > 0):
print bin(turn_it_on)


thanks for the code...it's legit code..:grin:

a = 0b10111011
mask = 0b10111
x = a | mask
print str(bin(x))