Why does my flip_bit function return the wrong number?


#1

Question

Why does my flip_bit function return the wrong number?

Answer

There are a few common mistakes with this exercise that are listed below:
Make sure your mask is created by shifting a 1 left by n - 1 digits. If we shift by n, that is one too many, since the 1 starts off in the first position.
The result you return is the result of XORing together the number passed to the function and the mask you just created.
Be sure to wrap the result in bin() when you return it!


FAQ: Learn Python - Introduction to Bitwise Operators - Slip and Slide