4.3 Bit manipulation

Bulk view disabled for Guests. View lessons individually.

Bit Manipulation: Shifts and Masks

1. Why Manipulate Individual Bits?

Sometimes a program needs to work below the level of whole numbers — setting a single flag, extracting a colour channel from a pixel, or testing one status bit returned by a device. Bit manipulation provides operations that act on the individual bits of a value, and they are extremely fast because the processor performs them directly.

Bit numbering. Bits are numbered from the right, starting at 0. In an 8-bit byte, bit 0 is the least significant bit (value 1) and bit 7 is the most significant bit (value 128).

2. Logical Shifts

A logical shift moves every bit left or right by a given number of places. Vacated positions are filled with 0, and bits shifted off the end are lost.

Logical shift left

Original 0 0 0 0 1 0 1 1 = 11 Shift left 1 0 0 0 1 0 1 1 0 = 22 (0 added on the right) Shift left 2 0 0 1 0 1 1 0 0 = 44

Each shift left multiplies by 2. Two places multiplies by 4, three places by 8.

Logical shift right

Original 0 0 0 0 1 0 1 1 = 11 Shift right 1 0 0 0 0 0 1 0 1 = 5 (the 1 is lost) Shift right 2 0 0 0 0 0 0 1 1 = 2

Each shift right divides by 2, discarding any remainder. 11 ÷ 2 = 5 remainder 1, and that remainder is lost permanently.

Shifting right loses data. Because the discarded bits cannot be recovered, shifting right then left does not return the original value. This is a favourite examination point.

3. Arithmetic Shifts

An arithmetic shift is used with signed numbers held in two's complement, where the leftmost bit is the sign bit. To preserve the sign, an arithmetic shift right copies the sign bit into the vacated position instead of inserting a 0.

Negative number in two's complement: Original 1 1 1 1 0 1 1 0 = -10 Logical shift right 1 0 1 1 1 1 0 1 1 = 123 WRONG - sign destroyed Arithmetic shift right 1 1 1 1 1 1 0 1 1 = -5 CORRECT - sign preserved
Logical shiftArithmetic shift
Used forUnsigned valuesSigned values (two's complement)
Shift right fills with0A copy of the sign bit
Shift left fills with00
Effect of right shiftUnsigned divide by 2Signed divide by 2, sign kept

4. Bit Masking

A mask is a carefully chosen binary pattern combined with a value using a logical operator, in order to isolate, set or invert particular bits. The choice of operator determines the effect.

GoalOperatorMask bitBecause
Test / isolate a bitAND1 where you want to keepX AND 1 = X, X AND 0 = 0
Set a bit to 1OR1 where you want to setX OR 1 = 1, X OR 0 = X
Invert / toggle a bitXOR1 where you want to flipX XOR 1 = NOT X, X XOR 0 = X

Testing a bit with AND

Is bit 2 of 01101101 set? Use a mask with a 1 in position 2 only:

Value 0 1 1 0 1 1 0 1 Mask 0 0 0 0 0 1 0 0 (bit 2 only) AND --------------- Result 0 0 0 0 0 1 0 0 non-zero, so bit 2 IS set

If the result is zero the bit was 0; if non-zero the bit was 1. Every other bit is forced to 0 by the mask, so only the bit of interest can influence the answer.

Setting a bit with OR

Value 0 1 1 0 1 0 0 1 Mask 0 0 0 0 0 1 0 0 (set bit 2) OR --------------- Result 0 1 1 0 1 1 0 1 bit 2 now 1, all others unchanged

Toggling bits with XOR

Value 0 1 1 0 1 0 0 1 Mask 1 1 1 1 1 1 1 1 (flip every bit) XOR --------------- Result 1 0 0 1 0 1 1 0 every bit inverted
Clearing a bit to 0 needs AND with a mask that has a 0 in that position and 1s everywhere else. To clear bit 2, AND with 11111011.

5. Putting It Together

Extracting the middle 4 bits of an 8-bit value uses a mask followed by a shift:

Value 1 0 1 1 0 1 1 0 Mask 0 0 1 1 1 1 0 0 isolate bits 2-5 AND --------------- 0 0 1 1 0 1 0 0 Shift right 2 0 0 0 0 1 1 0 1 = 13, the extracted value

The mask discards the unwanted bits; the shift moves the remainder down so it can be read as an ordinary number. This is exactly how a colour channel is extracted from a packed pixel value.

6. Exam Focus

Match the operator to the task. AND to test or clear, OR to set, XOR to toggle. Choosing the wrong operator is the most common error, and no marks follow from a correct method applied with the wrong one.
Say which shift and by how many places. "Shift it" is not an answer. Write "logical shift left by 3 places", and state the effect — multiplication by 8.
Use an arithmetic shift for signed values. If a question involves negative numbers in two's complement, a logical right shift will corrupt the sign. Name the arithmetic shift explicitly and explain that the sign bit is copied.

Quick self-check

  • State the effect on the value of a logical shift left by 4 places.
  • Perform a logical shift right by 2 on 10110100.
  • Explain why shifting right then left does not restore the original value.
  • Give the 8-bit mask and operator needed to test bit 5.
  • Give the mask and operator needed to clear bit 0 to zero.
  • Why is an arithmetic shift needed for two's complement numbers?