Edexcel International Advanced Level Computer Science, point 2.2.2
Definition
Bitwise manipulation means operating directly on the individual bits of a binary pattern, instead of on the number as a whole. The main tools are shifts and bit masks.
Function and why needed
- Shifts are a fast way to multiply and divide by powers of 2.
- Masks let a program test, set, clear or flip particular bits. Many things are stored as flags in a single byte or register: permissions, hardware control bits, pixel colour channels, network address parts, and the status register (1.1.4).
- Bitwise operations are very fast, since the ALU carries them out directly.
a. Logical shift
A logical shift moves all the bits left or right by a number of places. Zeros are shifted in, and the bits that fall off the end are lost.
Logical shift left
Each bit moves left, and 0s fill the right.
Shifting left by places multiplies by (as long as no 1s fall off the left end).
Logical shift right
Each bit moves right, and 0s fill the left.
Shifting right by places divides by , rounding down (the bits that fall off the right are lost). This is correct for unsigned numbers.
Problems
- Bits lost on the left when shifting left give a wrong answer:
10010000shifted left by 1 gives00100000, which is not double. - A logical right shift on a signed negative number gives a wrong answer, because the sign bit becomes 0. That's what the arithmetic shift fixes.
b. Arithmetic shift
An arithmetic shift is for signed (two's complement) numbers, and keeps the sign.
- Arithmetic shift right: bits move right, and the sign bit (MSB) is copied into the vacated place, so negative numbers stay negative.
- Arithmetic shift left: the same as a logical left shift (0s enter on the right). The result is wrong if it changes the sign bit (overflow, 2.2.3). For example,
11111010() shifted left 1 gives11110100(), which is correct; but01100000() shifted left 1 gives11000000, which reads as : the sign bit has changed, so this is overflow.
| Shift | Left | Right |
|---|---|---|
| Logical | 0 enters on the right | 0 enters on the left |
| Arithmetic | 0 enters on the right | Sign bit copied on the left |
Arithmetic right shift rounds towards negative infinity: (11111011) shifted right by 1 gives 11111101, which is , not .
c. Bit masks
A mask is a binary pattern combined with another using a logical operation, so that selected bits are tested or changed.
AND: test or clear bits
and . So AND with a mask clears the bits where the mask is 0, and keeps the bits where it is 1.
To check (test) a bit: AND with a mask that has 1 only in that position. If the result is not zero, the bit was set.
To clear the lower four bits:
OR: set bits
and . So OR with a mask sets the bits where the mask is 1, and leaves the others.
XOR: toggle (flip) bits
and . So XOR with a mask flips the bits where the mask is 1.
XOR also compares patterns: a result of all 0s means the two patterns are equal.
Summary
| Operation | Mask has 1 | Mask has 0 | Use |
|---|---|---|---|
| AND | Bit kept | Bit cleared | Test or clear bits |
| OR | Bit set | Bit kept | Set bits |
| XOR | Bit flipped | Bit kept | Toggle bits, compare |
Truth tables
| A | B | A AND B | A OR B | A XOR B |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Worked example
Task: a device status byte is 01101001. Bit 0 is the least significant. Bit 3 means "ready", bit 5 means "error". (a) Is the device ready? (b) Clear the error bit. (c) Set bit 7. (d) Multiply the original byte by 4.
(a) Test bit 3 with AND 00001000:
(b) Clear bit 5 with AND 11011111:
(c) Set bit 7 with OR 10000000:
(d) Shift left 2 places: 01101001 (105) becomes 10100100 (164). This is not , because 420 doesn't fit in 8 bits. The two leftmost bits (01) fell off the left end, and . So a left shift can overflow (2.2.3), and the programmer must check that the result still fits.
Exam tip: to check the status of a bit, AND with a mask that has a 1 only in that position: a non-zero result means the bit is 1. When masking, line the value and the mask up, and write the result under them. Remember the three jobs: AND tests or clears, OR sets, XOR flips. For shifts, say logical or arithmetic, and check for lost bits or a changed sign. Left by is and right by is .
Check yourself
What is the result of a logical shift left by 3 on 00000101?
00101000 (5 × 8 = 40).
What is the difference between a logical and an arithmetic right shift?
A logical right shift puts 0s in on the left. An arithmetic right shift copies the sign bit into the vacated places, so signed numbers keep their sign.
Which mask and operation would you use to set bit 6 of a byte?
OR with 01000000.
Which operation would you use to flip every bit in the low nibble?
XOR with 00001111.
How can you test whether bit 2 of a value is set?
AND the value with 00000100. If the result is not zero, bit 2 is set.