All subjects · Computer Science · WCP01 Principles of Computer Science
Bitwise manipulation: shifts and bit masks
Edexcel IAL Computer Science · WCP01 Principles of Computer Science
Shifts move every bit left or right; masks use AND, OR and XOR to check, set, clear or flip chosen bits (spec point 2.2.2).
Logical shifts
- A logical left shift moves every bit left; the leftmost bit is lost and a 0 enters on the right. Each place multiplies an unsigned number by 2 (if no 1s are lost).
- A logical right shift moves every bit right; the rightmost bit is lost and a 0 enters on the left. Each place divides an unsigned number by 2, discarding any remainder.
Example: 1011 0100 logical shift right 2 = 0010 1101; logical shift left 1 = 0110 1000.
Arithmetic shifts
An arithmetic shift preserves the sign of a two's complement number.
- Arithmetic right shift: bits move right and copies of the sign bit (MSB) fill the gap on the left. Each place divides a signed number by 2, rounding down. Example: 1011 0100 (-76) arithmetic shift right 2 = 1110 1101 (-19).
- Arithmetic left shift: bits move left and 0s enter on the right, multiplying by 2. If the sign bit changes, the result has overflowed.
Shifts are needed because they are much faster than general multiplication or division and are used to pack and unpack bits.
Bit masks
A mask is a bit pattern combined with data using a logical operator, bit by bit:
- AND with a mask: keeps bits where the mask has 1, clears bits where it has 0. Used to check (isolate) or clear bits. To test bit 2 of 1011 0100: AND with 0000 0100 gives 0000 0100; non-zero, so the bit is set.
- OR with a mask: sets the bits where the mask has 1, leaves the rest. 1011 0100 OR 0000 0011 = 1011 0111.
- XOR with a mask: flips (toggles) the bits where the mask has 1. 1011 0100 XOR 1111 0000 = 0100 0100.
Masks are used for flags in the status register, permissions and hardware control bits.
Worked example
A device status byte is 0110 1001. Bit 0 is the rightmost bit. Use a mask to check whether bit 3 is set.
- Mask with a 1 only in bit 3: 0000 1000
- AND: 0110 1001 AND 0000 1000 = 0000 1000
- The result is not zero
Answer: Bit 3 is set (1).
Check yourself
- Perform a logical right shift of 3 places on 1101 0110.
Show answer
00011010. Three 0s enter on the left: 0001 1010.
- Perform a logical left shift of 2 places on 0010 1101.
Show answer
10110100. Two 0s enter on the right: 1011 0100.
- The 8-bit two's complement number 1100 1000 is arithmetic shifted right by 2 places. What is the denary value of the result?
Show answer
-14. Result 1111 0010 = -14 (-56 / 4).
- The unsigned number 0001 0011 is logically shifted left 3 places. What is the denary value of the result?
Show answer
152. 19 x 23 = 152.
- Apply the mask 0000 1111 to 1011 0110 using AND.
Show answer
00000110. AND keeps only the low nibble: 0000 0110.
- Apply the mask 1000 0001 to 0101 0101 using XOR.
Show answer
11010100. The first and last bits are flipped: 1101 0100.
- Which operator and mask would set bit 7 (the leftmost bit) of a byte to 1 without changing the other bits?
- AND with 1000 0000
- AND with 0111 1111
- OR with 1000 0000
- XOR with 0111 1111
Show answer
OR with 1000 0000. OR with 1 forces a bit to 1; OR with 0 leaves bits unchanged.
- What is the difference between a logical and an arithmetic right shift?
- A logical right shift multiplies by 2; an arithmetic one divides
- An arithmetic right shift moves bits left
- A logical right shift keeps the sign of negative numbers
- An arithmetic right shift fills the gap with copies of the sign bit; a logical one fills it with 0s
Show answer
An arithmetic right shift fills the gap with copies of the sign bit; a logical one fills it with 0s. Copying the sign bit keeps negative numbers negative.
Sources
Last updated 8 November 2026. Spotted a mistake? Tell us.