How does bit shifting work?

Question

How does bit shifting work?

Answer

An easy way to look at bit shifting is to visually move the bits in the specified direction by the specified amount. If we have a bit string of 1000 and shift right by 1, we end up with 0100. Shift right by another 1 and we get 0010.
If we have 0010 and shift right by 2, however, we lose our bit! And there’s no getting it back after it falls of the beginning or end, so be careful! We’d end up with 0000.
So given some variable like my_bit_string = 0b1000, we can get 0b0001 by doing my_bit_string = my_bit_string >> 3.

2 Likes

Could someone explain the below statement as it confuses me a bit:

Shift operations are similar to rounding down after dividing and multiplying by 2 (respectively) for every time you shift, but it’s often easier just to think of it as shifting all the 1s and 0s left or right by the specified number of slots.

While the shift operations seem to be pretty easy to understand, I did not get the similarity between shifts and rounding.

5 Likes

Dear coding and Codecademy experts,

“Slide to the Left! Slide to the Right!” exercise explanation is quite confusing, could someone help me out to understand it? Googling on similar topics did not help…

Thanks

Kind regards,
Roman

I kinda thought the same thing. This always has been confusing to me as well so i usually don’t even try this stuff out.

Here is a link to some Python documentation on this subject that might help you understand how it all works.

If this does not help i would advice to create a new topic under get help since it is a very good question in my opinion. And seeing this is your second post it should get more attention as its own post.

4 Likes

You pointed me to the right direction to search, in the Python docs there is a page that gives a better explanation, so it turns out to be very simple:
https://wiki.python.org/moin/BitwiseOperators

x << y
Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.

x >> y
Returns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y.

So based on the binaries and shifting operations from exercise it will be like:

Shift right: 0b1100 / 2**0b10 => 0b1100 / 0b100 = 0b11
Shift left: 0b1 * 2**0b10 => 0b1 * 0b100 = 0b100

Rounding down is probably only a reference to the floating point results of Python division.

Thanks :slight_smile:

6 Likes

When I converted all this to integers, it also worked!

1 Like

I agree - I don’t think ‘rounding’ is a crucial part of this explanation.

1 Like

https://www.codecademy.com/courses/learn-python/lessons/introduction-to-bitwise-operators/exercises/slide-to-the-left-slide-to-the-right?action=resume_content_item

In the example on this page about shifting to the right:

Right Bit Shift (>>)

0b0010100 >> 3 == 0b000010 (20 >> 3 = 2)
0b0000010 >> 2 == 0b000000 (2 >> 2 = 0)

shouldn’t it be:
0b0010100 >> 3 == 0b0000010 (has one more 0 than the one in the example)
0b0000010 >> 2 == 0b0000000 (has one more 0 than the one in the example)

Please help,
Thanks!

1 Like

The 0s in the front don’t mean anything in binary. :slightly_smiling_face: