This question is from CSAPP CMU ICS 2015Fall Lecture 02 Bits, Bytes, and Integers 19:30 and the related scripts are
The other thing is confusing to people is
What should happen if you say I want to shift an 8-bit number 8 positions to the left
And x is a single byte what do you think you should get
Zero that would be a pretty logical thing you kind of shift all those bits out you fill them with zeros
----------- I don't understand -----------
On most machines you'll get whatever x was
Because what will do is it will compute this number mod 8
And the reason that happens is if you think about it
It's looking at just the lower two three bits of the shift amount and ignoring all the rest
So that's effectively like module 8
So that's just a warning and some machines it does
What you just thought it should and other machines it does this
And so there's no no guarantee and in C that it will be one way or the other
Randal E. Bryant says there are two results when shifting an 8-bit number 8 positions to the left. (Correct me if I'm wrong)
- zero
module 8unchanged
I place a horizontal rule to separate the part I don't understand. Especially On most machines you'll get whatever x was and Because what will do is it will compute this number mod 8, I don't understand why x is the same with x mod 8.
Could someone explain a bit more on the part below the horizontal rule, better with a runnable program?
There are some related questions on SO like What happens with bitwise shift for all 8 bits and Left shift operation on an unsigned 8 bit integer, but both didn't contain the module part.