Binary Shifts
A binary shift changes the positions of bits in a register. The register width stays fixed, so each shift type has a rule for the bit that enters an empty position and for any bit that leaves the register.
By the end of this page, you should be able to:
- perform logical, arithmetic and cyclic shifts left or right;
- state which bits are lost, introduced, copied or wrapped around;
- compare logical and arithmetic right shifts using the sign bit;
- keep the result at the original register width.
1. Keep the register width fixed
A shift moves bits left or right by a stated number of places. Bits do not change into a wider or narrower register. Any result must have the same number of bits as the starting value.
When a bit moves beyond one end of the register, what happens to it depends on the shift type. For a shift of more than one place, repeat the rule one place at a time.
Do not add extra register positions
Keep only the original register width. A bit shifted past the edge may be lost or may wrap around, depending on the shift type.
2. Logical shifts move bits and fill with zero
In a logical shift, every bit moves in the stated direction. A zero enters each vacant position. Any bit shifted beyond the register width is lost.
| Operation | Bit entering the register | Bit leaving the register |
|---|---|---|
| Logical shift left (LSL) | Zero enters at the right. | The leftmost bit is lost. |
| Logical shift right (LSR) | Zero enters at the left. | The rightmost bit is lost. |
For a shift of several places, apply the same movement repeatedly. Keep the result at the fixed width after each movement and note the outgoing bits that are lost.
3. Arithmetic shifts work with signed values
An arithmetic shift is used with a signed binary value. In an arithmetic right shift, the sign bit is copied into each new leftmost position. This helps keep the sign of the value.
In an arithmetic left shift, bits move left and zero enters on the right. The bit leaving the left end is lost. If this changes the sign of a signed value, the result may overflow the register's range.
| Operation | What enters | What to watch |
|---|---|---|
| Arithmetic right | A copy of the sign bit enters on the left. | The rightmost outgoing bit is lost. |
| Arithmetic left | Zero enters on the right. | The leftmost outgoing bit is lost; signed overflow may occur. |
4. Cyclic shifts rotate the bits
A cyclic shift rotates the bit pattern. The bit that leaves one end re-enters at the other end. No bit is lost and no new zero replaces it.
| Operation | Outgoing bit | Where it goes |
|---|---|---|
| Cyclic left | The leftmost bit | It wraps to the rightmost position. |
| Cyclic right | The rightmost bit | It wraps to the leftmost position. |
5. Compare the three shift types
| Shift type | What fills the open position? | What happens at the edge? |
|---|---|---|
| Logical | Zero | The outgoing bit is lost. |
| Arithmetic right | A copy of the sign bit on the left | The rightmost outgoing bit is lost. |
| Arithmetic left | Zero on the right | The leftmost outgoing bit is lost; signed overflow may occur. |
| Cyclic | The outgoing bit from the other end | The bit wraps around; none is lost. |
Interactive: shift a fixed-width register
Choose the shift type, direction and number of places. Then move one place at a time. This new eight-bit example lets you see the outgoing bit and the bit inserted or wrapped at each step.
6. Avoid these common mix-ups
| Mix-up | Correct explanation |
|---|---|
| Using zero for every right shift | Arithmetic right shift copies the sign bit into the leftmost position. |
| Keeping a bit that has moved beyond the register | A logical or arithmetic shift loses the outgoing bit. |
| Discarding the bit in a cyclic shift | A cyclic shift wraps the outgoing bit to the opposite end. |
| Changing the register width | Keep the result at the original width after every shift. |
| Calling the sign bit a new zero | For an arithmetic right shift, copy the original sign bit into each new leftmost position. |
7. Check your understanding
Try each question before opening the suggested response.
-
In a logical left shift, what enters at the right and what happens to the leftmost bit?
Suggested response
Zero enters at the right. The leftmost bit is shifted out and lost.
-
How does an arithmetic right shift differ from a logical right shift?
Suggested response
A logical right shift inserts zero at the left. An arithmetic right shift copies the sign bit into the leftmost position.
-
What happens to the rightmost bit in a cyclic right shift?
Suggested response
It wraps around and enters at the leftmost position. No bit is lost.
-
Why must a shifted result keep the original register width?
Suggested response
A shift changes the positions of bits within the register; it does not change the register's number of bit positions.
-
What risk can occur after an arithmetic left shift of a signed value?
Suggested response
A bit can be lost at the left edge and the sign may change, so the signed result may overflow the range represented by the register.
Final check
Show a result at the stated width and explain the edge bits. For a right-shift comparison, identify whether zero or the sign bit enters at the left. For a cyclic shift, state which bit wraps around.