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).

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Logical shifts

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.

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:

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.

  1. Mask with a 1 only in bit 3: 0000 1000
  2. AND: 0110 1001 AND 0000 1000 = 0000 1000
  3. The result is not zero

Answer: Bit 3 is set (1).

Key idea: Logical shift: 0s fill the gap. Arithmetic right shift: copies of the sign bit fill. AND mask checks or clears, OR sets, XOR toggles.

Check yourself

  1. Perform a logical right shift of 3 places on 1101 0110.
    Show answer

    00011010. Three 0s enter on the left: 0001 1010.

  2. Perform a logical left shift of 2 places on 0010 1101.
    Show answer

    10110100. Two 0s enter on the right: 1011 0100.

  3. 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).

  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.

  5. Apply the mask 0000 1111 to 1011 0110 using AND.
    Show answer

    00000110. AND keeps only the low nibble: 0000 0110.

  6. Apply the mask 1000 0001 to 0101 0101 using XOR.
    Show answer

    11010100. The first and last bits are flipped: 1101 0100.

  7. 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.

  8. 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.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.