Logical right‑shift (>>> in Java, >> on unsigned values in C)
Arithmetic right‑shift (>> on signed two’s‑complement values)
Apply shifts in typical algorithmic, data‑representation and low‑level programming contexts.
Why Shifts Matter
Multiplication / division by powers of two – a single CPU instruction.
Core of many algorithms (cryptography, graphics, compression, fast exponentiation).
Essential for extracting, inserting or packing bit‑fields in data structures.
Direct mapping to assembly instructions: LSL (logical shift left), LSR (logical shift right), ASR (arithmetic shift right).
Basic Definition
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.
Types of Shifts
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 –∞)
Mathematical Interpretation
For an n‑bit unsigned integerx 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 integery:
\[
y \gg k = \text{sign‑extend}\!\left(\big\lfloor \tfrac{y}{2^{k}} \big\rfloor\right)
\]
Behaviour Illustrated
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)
Overflow & Under‑flow
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₂).
Masking & Field Extraction Using Shifts
Shifts are almost always paired with & (AND) or | (OR) to isolate or reposition sub‑fields.
Extract a 5‑bit colour component (bits 16‑20 of a 24‑bit RGB value):
mask = 0b11111 << 16; // 0x001F_0000
red = (rgb & mask) >> 16; // now in bits 0‑4
Insert a 3‑bit flag into bits 5‑7 of an unsigned integer n:
flag = 0b101; // value to insert
n = (n & ~ (0b111 << 5)) | (flag << 5);
Algorithmic Use of Shifts
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;
}
Cambridge Syllabus Links (AS 4.3 AO‑1 & AO‑2)
Multiplying an unsigned integer by 2k → x << k.
Dividing an unsigned integer by 2k → x >>> k.
Dividing a signed integer by 2k with rounding toward –∞ → y >> k.
Masking and field extraction → x & ((1 << k) - 1).
Understanding logical vs. arithmetic shift underpins the “CPU Architecture” (4.1) and “Assembly Language” (4.4) topics.
Common Pitfalls
Shifting by an amount ≥ the word size (n) is undefined in C and Java.
Confusing logical and arithmetic right shifts for signed values – the sign may change unexpectedly.
Ignoring overflow on left‑shifts; high‑order bits are discarded, possibly changing the sign of a signed value.
Assuming identical behaviour across languages: Java’s >>> is logical, while C’s >> on signed types is implementation‑defined.
Cross‑Topic Links
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:
Boolean algebra and Karnaugh maps (optimising combinational logic).
RISC vs. CISC processor design (pipeline and parallelism).
Data compression techniques that rely on bit‑level packing.
Cryptographic primitives that use circular shifts (e.g., ROT‑13, SHA‑1).
Practice Questions
Given the 16‑bit unsigned value 0x0F3A, compute 0x0F3A << 4 and give the result in hexadecimal.
For the signed 8‑bit two’s‑complement number 1001 0110₂, perform an arithmetic right shift by 1. State the resulting binary and decimal values.
Explain why 1100 0001₂ >>> 2 yields a different result from 1100 0001₂ >> 2 when the operand is interpreted as signed.
Write pseudocode that extracts bits 5‑7 (LSB = bit 0) from an unsigned integer n using shift and mask operations.
Show how a left‑shift can be used to multiply the unsigned 8‑bit value 0010 1101₂ (45) by 8, and indicate whether any overflow occurs.
(A‑level extension) A 32‑bit signed integer -12345678 is stored in two’s‑complement form. What is the result of an arithmetic right shift by 3? Show the intermediate binary representation.
Answer Key (Teacher Use)
0x0F3A << 4 = 0xF3A0
Binary: 1111 0011 1010 0000. The high‑order nibble “0” is discarded.
Arithmetic right shift: 1001 0110 → 1100 1011
Decimal: -53 (since 1100 1011₂ = –53 in two’s‑complement).
Arithmetic right shift by 3 → 1111 1001 0111 1001 0111 0001 0001 0001₂ = -1543209.
Suggested diagram: Bit positions before and after a left shift – the two left‑most bits are discarded, zeros are inserted on the right.
Context Within the Cambridge Computer Science Syllabus (2026)
AS‑Level Topics (Sections 1‑12)
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.
1. Information Representation – binary, hexadecimal, BCD, ASCII/Unicode.