Students will be able to:
<<)>>> in Java, >> on unsigned values in C)>> on signed two’s‑complement values)LSL (logical shift left), LSR (logical shift right), ASR (arithmetic shift right).A binary shift moves every bit of a binary representation a fixed number of positions left or right. Vacated positions are filled with a defined value (0 or the sign‑bit) and bits that move beyond the word size are discarded.
| Shift | Symbol (Java / C) | Direction | Fill‑in bits | Typical use |
|---|---|---|---|---|
| Logical left shift | << | towards more significant positions | 0 inserted into least‑significant bits (LSBs) | multiply unsigned value by 2k |
| Logical right shift | >>> (Java) / >> on unsigned (C) | towards less significant positions | 0 inserted into most‑significant bits (MSBs) | divide unsigned value by 2k |
| Arithmetic right shift | >> (both languages on signed types) | towards less significant positions | sign‑bit replicated (sign‑extension) | divide signed two’s‑complement value by 2k (rounding toward –∞) |
For an n‑bit unsigned integer x and shift amount k (0 ≤ k < n):
\[
x \ll k = (x \times 2^{k}) \bmod 2^{n}
\qquad\qquad
x \ggg k = \big\lfloor \tfrac{x}{2^{k}} \big\rfloor
\]
For a signed two’s‑complement integer y:
\[
y \gg k = \text{sign‑extend}\!\left(\big\lfloor \tfrac{y}{2^{k}} \big\rfloor\right)
\]
| Operand | Logical left (<< 2) | Logical right (>>> 2) | Arithmetic right (>> 2) |
|---|---|---|---|
Unsigned 8‑bit 0101 1010₂ (90) | 1011 0100₂ (180) | 0001 0101₂ (5) | same as logical right (5) – sign‑bit = 0 |
Signed‑positive 8‑bit 0010 1100₂ (44) | 1011 0000₂ (176) – overflow of MSB | 0000 1011₂ (11) | 0000 1011₂ (11) |
Signed‑negative 8‑bit 1110 1100₂ (‑20) | 1011 0000₂ (176) – sign lost! | 0011 1011₂ (59) | 1111 1011₂ (‑5) |
Left‑shifting discards the most‑significant bits that move beyond the word size.
unsigned 8‑bit: 1110 1101 << 2 → 0110 1100
Explanation: the two left‑most bits “11” are lost; the result is 108 decimal,
not 573 (which would be 1110 1101 00₂).
Shifts are almost always paired with & (AND) or | (OR) to isolate or reposition sub‑fields.
mask = 0b11111 << 16; // 0x001F_0000
red = (rgb & mask) >> 16; // now in bits 0‑4
n:flag = 0b101; // value to insert
n = (n & ~ (0b111 << 5)) | (flag << 5);
Because a shift corresponds to multiplication or division by a power of two, they appear in many fast‑arithmetic algorithms.
// Compute 2^k using a left‑shift (k ≥ 0)
int powerOfTwo(int k) {
return 1 << k; // 1 × 2^k
}
// Binary exponentiation (modular power)
int powMod(int base, int exp, int mod) {
int result = 1;
while (exp > 0) {
if (exp & 1) result = (result * base) % mod;
base = (base << 1) % mod; // base = base × 2
exp = exp >> 1; // exp = exp ÷ 2
}
return result;
}
x << k.x >>> k.y >> k.x & ((1 << k) - 1).>>> is logical, while C’s >> on signed types is implementation‑defined.In assembly language the same operations appear as LSL (logical shift left), LSR (logical shift right) and ASR (arithmetic shift right). Mastery of binary shifts therefore supports the “CPU Architecture” (4.1) and “Assembly Language” (4.4) sections of the syllabus, and provides a foundation for later A‑level topics such as:
0x0F3A, compute 0x0F3A << 4 and give the result in hexadecimal.1001 0110₂, perform an arithmetic right shift by 1. State the resulting binary and decimal values.1100 0001₂ >>> 2 yields a different result from 1100 0001₂ >> 2 when the operand is interpreted as signed.n using shift and mask operations.0010 1101₂ (45) by 8, and indicate whether any overflow occurs.-12345678 is stored in two’s‑complement form. What is the result of an arithmetic right shift by 3? Show the intermediate binary representation.0x0F3A << 4 = 0xF3A0 Binary: 1111 0011 1010 0000. The high‑order nibble “0” is discarded.
1001 0110 → 1100 1011 Decimal: -53 (since 1100 1011₂ = –53 in two’s‑complement).
1100 0001₂ >>> 2 = 0011 0000₂ (48). Arithmetic right shift replicates the sign bit (1): 1100 0001₂ >> 2 = 1111 0000₂ (‑16). The sign‑extension changes the value.
mask = 0b111 << 5; // 0b11100000
extracted = (n & mask) >> 5; // bits 5‑7 now in positions 0‑2
0010 1101 << 3 → 0101 1010 (binary 0101 1010₂ = 90).
No overflow occurs because the original MSBs are 0; the result fits in 8 bits.
-12345678 = 1111 0010 1111 0010 1110 0010 0010 0010₂. Arithmetic right shift by 3 → 1111 1001 0111 1001 0111 0001 0001 0001₂ = -1543209.
The following blocks are required for the full AS course. Only the binary‑shift block is expanded here; teachers should ensure the other blocks are taught alongside.
These topics build on the AS foundation and are examined at A‑level.
<<, >> and >>> are the CPU instructions LSL, LSR and ASR.Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.