9618 · 4.3
Bit manipulation flashcards
Revision flashcards for Cambridge 9618 Bit manipulation (syllabus 4.3). Flip, recall, then mark a real past-paper question.
Card
What is a Logical Shift Left (LSL)?
An operation that shifts all bits in a binary number one or more positions to the left. The most significant bit(s) are discarded, and the least significant bit position(s) are filled with 0s.
Card
What is the mathematical effect of an LSL by n places?
Multiplication by $2^n$. For example, LSL 1 multiplies by 2, LSL 3 multiplies by 8.
Card
What is a Logical Shift Right (LSR)?
An operation that shifts all bits in a binary number one or more positions to the right. The least significant bit(s) are discarded, and the most significant bit position(s) are filled with 0s.
Card
What is the mathematical effect of an LSR by n places on an unsigned integer?
Integer division by $2^n$. For example, LSR 1 divides by 2, discarding any remainder.
Card
What is an Arithmetic Shift Right (ASR)?
A shift operation for signed (two's complement) numbers. It shifts bits to the right, but the most significant bit (the sign bit) is copied into the newly vacant position, preserving the number's sign.
Card
What is the key difference between LSR and ASR?
LSR always fills the vacant MSB position with a 0. ASR fills it by copying the original sign bit (which can be 0 or 1). ASR is for signed numbers, LSR for unsigned.
Card
Is there an Arithmetic Shift Left (ASL)?
Yes, but its behaviour is identical to Logical Shift Left (LSL). The vacant LSB is always filled with a 0. For exam purposes, ASL and LSL are the same.
Card
What is a 'mask' in bit manipulation?
A binary value (a bit pattern) used with a logical operator (AND, OR, XOR) to modify another binary value. The mask determines which bits are affected.
Card
How do you use a mask with AND to check if bit 3 (from the right, starting at 0) is set in a byte `B`?
Perform `B AND 00001000`. If the result is non-zero (`00001000`), the bit was set. If the result is zero, the bit was not set.
Card
How do you use a mask with OR to set bit 5 of a byte `B` to 1?
Perform `B OR 00100000`. This forces bit 5 to be 1, leaving all other bits in `B` unchanged.
Card
How do you use a mask with XOR to toggle (flip) the lower four bits of a byte `B`?
Perform `B XOR 00001111`. This will flip the state of bits 0, 1, 2, and 3, while leaving the upper four bits unchanged.
Card
Common Trap: What happens if a logical shift left causes an important bit to be lost?
This is called an overflow. For example, shifting the signed 8-bit number for 65 (`01000001`) left by one place results in `10000010`, which is -126 in two's complement, not 130. The result is incorrect.