14. Slip and Slide


#1

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

Oops, try again. Your function fails on flip_bit('0b111', 2). It returns "0b11" when it should return "0b101".

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

#2

def flip_bit(number, n):
    mask = (0b1 << n) # should be n-1. n is like index and indexes start at 0.
    result = number ^ mask
    return bin(result)

#3

Ahh, that worked. Thanks @datfatcat.


#4

should be 0b1 << n - 1


#5

That's what I said :wink:


#6

@datfatcat yes sorry about that i didn't read your comment


#7

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