Bit manipulation

Computer Science AS · Processor fundamentals. The idea in plain words, the rules to remember, the mistake to avoid and a worked example, written and checked by Mega Lecture.

Test this topic freeAll Computer Science AS topics

The idea

A shift moves every bit in a register a number of places left or right. The three kinds differ in what enters the empty places. A logical shift puts in zeros. An arithmetic shift keeps the sign of a two's complement number. A cyclic shift sends each bit that leaves one end back in at the other end.

A mask is a bit pattern used with AND, OR or XOR to test or change chosen bits and leave the others alone. Bits are numbered from the right, starting at bit 0.

This is how devices are monitored and controlled: each bit of a byte can stand for one sensor or one switch.

Rules to remember

Common mistake

Students use OR when they want to clear a bit. OR can only set bits to 1; to clear a bit, use AND with a mask that has 0 in that position and 1 everywhere else.

Worked example

The accumulator holds 01101001. Which operation clears bit 5 (place value 32) to 0 without changing any other bit, and what is the result?

  1. To clear a bit, use AND with a mask that has 0 in that position and 1 in every other position.
  2. Bit 5 is the third bit from the left, so the mask is 11011111.
  3. 01101001 AND 11011111: bit 5 becomes 0 and the other seven bits stay the same, giving 01001001.

Answer: AND B11011111 gives 01001001.

Ask about this

Type what you are stuck on. The helper finds the explanation, worked examples and the notes to read, free and instant.

Keep going

← Assembly languageOperating systems →Take a Computer Science AS test