14/14 slip and slice


#1



https://www.codecademy.com/en/courses/python-intermediate-en-KE1UJ/2/4

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


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


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

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


Replace this line with your code.


#2

Order of parameters is determined by order of arguments. The number is given as the first parameter, the shift by the second parameter. When n is used in the first parameter, the usage needs to reflect that.


#3

I am sorry , didn't get you as i am new to programming .. Would you elaborate ?


#4

When we call the function, our number is passed in as the first argument, and the bit number as the second argument. The shift is computed against the bit number.

print flip_bit(0b111, 2)

should return, 0b101.

Your incorrect example is taking the number 2, and shifting it 0b111 - 0b1, which is,

0b10 << 6

which will return,

0b10000000

#5

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