TURN IT ON, help please!


#1


I followed the instruction step by step. what is wrong here? Thanks!

a = 0b10111011
mask = 0b011 
desired = a | mask
print bin(desired)

Oops, try again. The correct binary string was not printed!


#2

Hi, @orouge ,

You have this assignment to mask ...

mask = 0b011

However, when you use the | operator on the next line, a 0 in the mask variable will not turn on the corresponding bit from a. A 1 is what will turn on a corresponding bit from a.

You need to turn on the third bit as you count from the right, and only that bit. Taking that into account, the value you are assigning to mask needs to be revised. After the 0b prefix, you currently have a 0 where you need to have 1 instead, and you have a 1 wherever you need to have 0 instead.


#3

Thank you @appylpye but i am still a bit confused. Bear with me please. When it says the third bit from the right (does it mean --third bit only a = 0b10111(0)11 or all last three digits of a = 0b10111(011) ) and ALSO did you change 0s to 1s and vice verse in order to turn it on? English is my second language, sorry didn't understand the instruction well.


#4

@orouge ,

It means that you should turn on the "... third bit only a = 0b10111(0)11 ...", and leave all the other bits as they are given.

So you need to create a mask variable that turns that third bit on, but does not change any of the other bits. Consider the action of the | operator. Wherever there is a 0 in the mask variable, the action of a | mask will place whatever is in a into the result. Wherever there is a 1 in the mask variable, the action of a | mask will place a 1 into the result, that is, it will turn the corresponding bit on. So, there is only one place where you should have a 1 in the mask variable, and that is the third bit from the right.


#5

Thanks @appylpye but when we did 0b100 after 0b zero turned into 1 so turn it on and also two 1s turned into 00. result = 0b100

I am trying to understand third one is on as 1 and however first and second one became 0s

so to switch them off we make them zeros while third one is turned on as 1. right? Thanks beforehand


#6

Hi @orouge ,

After you have revised mask, you have this correct code ...

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

The output is ...

0b10111111

The reason we place two 0s at the right end of mask is that the only bit we want to change in going from a to desired is the third bit from the right. Since we are using a | operation, any 0s in the mask will produce the same bit value in desired as occurs in the corresponding bit in a. Where there is a 1 in a, a 0 in mask will produce a 1 in the corresponding position in desired. Where there is a 0 in a, a 0 in mask will produce a 0 in the corresponding position in desired. However, the 1 in the third position from the right in mask will produce a 1 in the same position in desired, regardless of what bit value occurs in that position in a, and that is what we want - to turn on the third bit from the right, and leave everything else as is.


#7

it is very clear @appylpye. THANK you. it is much APPRECIATED!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.