Flip and slide


#1



https://www.codecademy.com/courses/python-intermediate-en-KE1UJ/2/4?curriculum_id=4f89dab3d788890003000096#


It doesn't work


def flip_bit(number , n):
    result = (number ^ n)
    return bin(result).


OUTPUT : Oops, try again. Your function fails on flip_bit('0b1010101', 3). It returns "0b1010110" when it should return "0b1010001"
Can somebody explain the subject to me how ^ and | and & work? I don't understand it, I read the info Codecademy's exercises wrote but I didn't really get it


#2

You need to create a variable mask that flips the bitwise operator and stores that operation in a variable result that includes your number variable along with the mask,

mask = (0b1 << n-1)
result = number ^ mask

#3

Already passed it but thanks anyway


#4

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