12/14


#1

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

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


#2

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


#3

Try this...

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


#4

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


#5

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


#6

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:


#7

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


#8

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


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