14/14 Anybody?


#1

Does anyone could help me please with this Ex?


#2

Try shifting by one less than what you think you should be shifting.

mask = (0b1 << (n-1))


#3

I was trying to do this in many different ways,
but I'm still a bit confused,
the problem is that I don't understeand this exercise,
previous examples were OK for me.
that's my code for now:

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


#4

Let's look at what you're doing.

On line 1, you defined a function called, "flip_bit". Your function takes two arguments: "mask" and "n".

Line2.... mask = (0b1 << (n-1)) #sets mask equal to binary expression 10 (2 if we count by 10s because binary 0 = 0, binary 1 = 1, and binary 10 = 2)

Line3.... result = mask ^ n # this translates to performing the follow operation 0b10 ^ 0b10

Line4.... return bin(result) # the result of the above is 0b0 because the XOR operation produces "true" or 1, if and only if there is a difference in bits.

In plain English....

On line 2, you take the argument "mask" passed into the function "flip_bit", and you shift the 1s bit in mask by the argument "n" decremented by 1.

On line 3, you create a new variable called "result" and you set it equal to the result of running an XOR operation on the result of line 2, and the argument "n".

On line 4, you return the binary expression of the output of line 3.

You really only have one problem.

You take as an argument "mask", when you really want to take in a number and then use a mask to "check" that number for an expression in one position.


#5

Thank's a lot!
I've finally got it;)


#6

Can anybody provide the exact solution?


#7

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

print flip_bit(0b001, 2)


#8

thanks a lot for posting the answer...
could you please explain me why in the 2nd line, mask = (0b1 << (n-1)), we should write 0b1 and not something else?


#9

Thanks for posting the answer chaps40. I was trying to combine two operations into one line and it kept failing at one point or another. Once I saw your answer, it made it clear where I was going wrong and I stopped trying to do that.


#10

Thanks exactly what I am having issues with too why not 0b0 or 0b01 or anything else?


#11

thanks, but still..I can't get it well :grin:


#12

I had issues with this one as well. I just forgot to subtract the 1 after n. It makes sense now!


#13

Can you explain to my why the code is written like this on line 2:
mask = (0b1 << (n-1))
and not:
mask = (0b1 << n)

What is the purpose of adding -1?


#14

You only want to shift over 1 place because you start you mask (0b1 in the ones column)


#15

Took me a few to work it out:

mask = 0b1 << (n - 1) ---- Would equal 0b10

mask = 0b1 << n --------Would equal 0b100

Our function is trying to flip the n(th) bit, but if we don't subtract 1, we would be modifying the wrong place value with our XOR.

If we call flip_bit('0b111', 2) it should return 0b101 and not: 0b11

Though, both work in the console.


#16

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