Bitwise Manipulation & Masks (OCR A-Level Computer Science): Revision Notes
Bitwise Manipulation & Masks
Overview
Bitwise manipulation involves working directly with the binary representation of data. This is often used in low-level programming tasks, such as optimising performance or controlling hardware. Masks are specific binary patterns applied using bitwise operators to isolate, modify, or combine certain bits within binary data. Understanding bitwise operations and how to apply masks is essential for efficient programming and problem-solving in Computer Science.
Bitwise Operators
Bitwise operators perform operations on individual bits of binary numbers.
AND (&****):
- Compares corresponding bits of two binary numbers.
- Result is 1 if both bits are 1; otherwise, 0.
- Use: Isolate specific bits using masks.
Example:
1101 (13)
& 1011 (11)
------
1001 (9)
OR (|****):
- Compares corresponding bits of two binary numbers.
- Result is 1 if at least one bit is 1; otherwise, 0.
- Use: Set specific bits to 1.
Example:
1101 (13)
| 1010 (10)
------
1111 (15)
XOR (^****):
- Compares corresponding bits of two binary numbers.
- Result is 1 if the bits are different; otherwise, 0.
- Use: Toggle specific bits (flip between 0 and 1).
Example:
1101 (13)
^ 1010 (10)
------
0111 (7)
NOT (~****):
- Inverts all bits (0 becomes 1, 1 becomes 0).
- Use: Bitwise negation (not widely used with masks).
Bit Shifts
Shifts move all bits in a binary number to the left or right.
Left Shift (<<****):
- Moves bits to the left by a specified number of positions.
- Inserts 0s on the right.
- Effectively multiplies the number by (2^n), where (n) is the number of shifts.
Example:
0011 << 2 = 1100
Right Shift (>>****):
- Moves bits to the right by a specified number of positions.
- Inserts 0s on the left.
- Effectively divides the number by (2^n) (integer division).
Example:
1100 >> 2 = 0011
Bit Masks
A mask is a binary pattern used with bitwise operators to manipulate specific bits in another binary number.
Purpose of Masks:
- AND Mask: Isolate specific bits.
- OR Mask: Set specific bits to 1.
- XOR Mask: Toggle specific bits.
Examples
Example 1: Using an AND Mask to Isolate Bits Isolate the last 4 bits of the number 10111011 using an AND mask.
- Number: 10111011
- Mask: 00001111 (AND mask to isolate last 4 bits)
Operation:
10111011
& 00001111
----------
00001011
Result:
1011 (decimal 11).
Example 2: Using an OR Mask to Set Bits Set the last 3 bits of 10100000 to 1.
- Number: 10100000
- Mask: 00000111 (OR mask to set last 3 bits)
Operation:
10100000
| 00000111
----------
10100111
Result:
10100111.
Example 3: Using an XOR Mask to Toggle Bits Toggle the last 4 bits of 11110000.
- Number: 11110000
- Mask: 00001111 (XOR mask to toggle last 4 bits)
Operation:
11110000
^ 00001111
----------
11111111
Result:
11111111.
Example 4: Left Shift Perform a left shift of 2 positions on 00001101.
Operation:
00001101 << 2 = 00110100
Result:
00110100 (decimal 52).
Example 5: Right Shift Perform a right shift of 3 positions on 10110000.
Operation:
10110000 >> 3 = 00010110
Result:
00010110 (decimal 22).
Note Summary
Common Mistakes
- Forgetting to Align Bits in Masks: Ensure masks are aligned correctly for the operation (e.g., isolating specific bits).
- Incorrect Shift Interpretation: Remember that left shifts multiply and right shifts divide, and this applies only to unsigned binary.
- Misunderstanding XOR: XOR toggles bits, so reapplying the same XOR mask will revert the changes.
Key Takeaways
- Bitwise Operators:
- AND: Isolate bits.
- OR: Set bits to 1.
- XOR: Toggle bits.
- Shifts:
- Left shifts multiply by
- Right shifts divide by
- Masks: Allow precise control over specific bits using bitwise operators, useful for low-level programming and optimisation.