14. Slip and Slide - Why do I have to put 0b1?

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

I thought I had to put number which would be inputed where I put 0b1 in that code bellow. But it wasn’t working so I came in here and everybody was putting 0b1, so I did it too and it work. But why?

<Below this line, add a link to the EXACT exercise that you are stuck at.>

https://www.codecademy.com/en/courses/python-intermediate-en-KE1UJ/2/4
<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>

```python

def flip_bit(number, n):
mask = (0b1 << n - 1)
result = number ^ mask
return bin(result)

<do not remove the three backticks above>

0b1 is the binary integer, 1. If this code shifts the 1 bit two magnitudes to the left. it is the same as multiplying by 2, twice. The result should be 4, or 0b100.

3 Likes

Let us assume that number = 186 (0b10111010) and we want to flip the third bit from the right.

From the exercise about the XOR operator you should know that we need mask 0b100 to achieve this task. And that’s exactly what (0b1 << 3 - 1) will return.

3 Likes

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