A left shift moves bits toward higher positions and fills the right with zeroes. In our fixed-width word model, bits leaving the left edge are discarded. An 8-bit 10000001 shifted left by one becomes 00000010.
Logical right shift fills the left with zeroes. Arithmetic right shift repeats the sign bit. Thus 10000000 shifted right by one is 01000000 logically, but 11000000 arithmetically.
These drills use an explicit word model. C has additional rules: small integers can be promoted before a shift; shift counts must be within the promoted width; signed left shifts can be undefined. In C17, right-shifting a negative signed value is implementation-defined.
Explain it before you practise
Look away from the example. Explain why it works and predict a boundary case. Then check your reasoning.
Shift 10000001 right by one, logically and arithmetically. When do the two shifts agree?
Check your reasoning
Logical shift gives 01000000; arithmetic shift gives 11000000. They agree when the original sign bit is 0 (or the shift count is zero), because both then fill with the same bits.
Cement this knowledge
Solve ten fresh questions without the worked example. Mistakes return during the session; spaced review helps you recall the method on later days.
Practice this topic →