Slip and Slide: The way the instructions are presented is not clear


#1

I really enjoyed the INTRODUCTION TO BITWISE OPERATORS up until Slip and Slide:
https://www.codecademy.com/en/courses/learn-python/lessons/introduction-to-bitwise-operators/exercises/slip-and-slide

When I read the instructions I was very confused as to what was expected. I got the correct code and I understand it without a problem. To be honest, I got the correct code after giving up because I was simply not understanding the question at all but I knew I would be able to interpret the correct code.

While studying the answer I did learn something new; that 0b1 << (n -1) can return a result, which is cool and an imperative to solve the problem.

Below is an excerpt from the instructions. The bold text, specifically the section that is emphasized confused me:

Flip the nth bit (with the ones bit being the first bit) and store it in result.’

Could someone rephrase the instructions in a way that is…clearer or less ambigous to someone who speaks English as a third language? I would really appreciate it.


#2

That is cool, but what did you actually learn about it? What does than line of code do?


#3

Hello again mtf :slight_smile:

This is what I learned.

First, I learned that by giving just one bit (0b1) and a designated number of slots to shift I can get (via return) an interger value equal to the binary :

A shift of 0 slots: 0b1 = Binary value 1

A left shift of 1 slot: 0b10 = Binary value 2

A left shift of 2 slots: 0b100 = Binary value 4

A left shift of 3 slots: 0b1000 = Binary value 8

A left shift of 4 slots: 0b10000 = Binary value 16

Second, the shift needs the n-1 to ensure that the bit we want to flip is actually the first, second, third etc. because without that n-1 the bit flipped would be located one slot ahead since normally a shift occurs right after the furthest right slot as seen in my examples above, starting from the left shift of 1 slot.

Now, about my confusion. Below is an excerpt from the instructions. The bold text, specifically the section that is emphasized confused me:

‘Flip the nth bit (with the ones bit being the first bit) and store it in result.’

Could you present the question in a way that would be super easy to interpret? Also, what does nth mean in the context of the question? Yeah, the more I read it the more confused I feel.

I really appreciate your help!


#4
A left shift of 0 bits: 0b1     => mask for bit 1

A left shift of 1 bit:  0b10    => mask for bit 2

A left shift of 2 bits: 0b100   => mask for bit 3

A left shift of 3 bits: 0b1000  => mask for bit 4

A left shift of 4 bits: 0b10000 => mask for bit 5

When the n is 5, it is the fifth bit from the right. That is what is meant by the nth bit. The above bolded statement is telling us to not forget the first bit, hence zero shift when n is 1.

The nth bit from the right is a shift left of n - 1.


#5

Ohhhhh! Ok, I get it :slight_smile:

Your explanation is great!

Thank you so much for your help!


#6

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