📑 Contents

Chapter 4.3: Bit Manipulation

9618 AS Computer Science

📚 Learning Objectives
🌟 Did You Know?

Bit manipulation is one of the fastest operations a computer can perform. By using simple binary tricks, we can achieve substantial processing efficiency in control systems. Each bit in a byte can represent a different device or feature - turning lights on, checking sensors, or flagging errors!

8-bit Register (1 Byte) 1 0 1 1 0 0 1 0 7 6 5 4 3 2 1 0 ← MSB LSB →

1. Binary Shifts Overview

A binary shift is an operation that moves all the bits in a binary number left or right by a certain number of positions. This is often used in programming for fast multiplication or division by powers of 2.

📖 What is a Shift Operation?

1.1 Three Types of Shifts

Shift Type Description Best For
Logical Shift Moves bits left/right, fills gaps with 0s Unsigned numbers, raw bit manipulation
Arithmetic Shift Moves bits, preserves sign bit (MSB) Signed numbers (two's complement)
Cyclic Shift Rotates bits around, nothing is lost Encryption, checksums, bit rotation
Shift Directions LEFT SHIFT (<<) Bits move toward MSB RIGHT SHIFT (>>) Bits move toward LSB Left Shift = Multiply by 2 | Right Shift = Divide by 2
💡 Exam Tip

Remember: Left shift multiplies by 2 for each position shifted. Right shift divides by 2 for each position shifted. This is why shifts are much faster than actual multiplication or division operations!

2. Logical Shifts

Logical shifts move bits left or right and fill the empty positions with zeros. These are used for unsigned binary numbers or raw bit manipulation.

2.1 Logical Left Shift

In a logical left shift, each bit is moved to the left. The empty space on the right is filled with zero, and the leftmost bit (MSB) that falls off is discarded.

📝 Logical Left Shift Example

Original: 000110 (binary) = 6 (decimal)

Shift left by 2: 000110 << 2

Result: 011000 (binary) = 24 (decimal)

Notice: 6 × 2² = 6 × 4 = 24 ✓ Each left shift doubles the value!

Logical Left Shift (<< 2) Original: 0 0 0 1 1 0 << 2 Result: 1 1 0 0 0 0 Green = Zeros added ← Discarded bits fall off left

2.2 Logical Right Shift

In a logical right shift, each bit is moved to the right. The empty space on the left (MSB position) is filled with zero, and the rightmost bit (LSB) that falls off is discarded.

📝 Logical Right Shift Example

Original: 0011001 (binary) = 25 (decimal)

Shift right by 1: 0011001 >> 1

Result: 0001100 (binary) = 12 (decimal)

Notice: 25 ÷ 2 = 12.5 → 12 (integer division) ✓ Each right shift halves the value!

Logical Right Shift (>> 1) Original: 0 0 1 1 0 0 1 1 >> 1 Result: 0 0 0 1 1 0 0 0 Green = Zero added Discarded →
Logical Shift Direction What Happens Effect on Value
Left (<<) All bits shift left, 0 fills rightmost bit Multiplies by 2 for each position
Right (>>) All bits shift right, 0 fills leftmost bit Divides by 2 for each position
⚠️ Important Note

Some processors do not have a built-in logical right shift - they only have an arithmetic right shift. Understanding the difference is crucial for handling signed vs unsigned numbers!

💡 Quick Calculation Trick

For a left shift of n positions: Value × 2ⁿ

For a right shift of n positions: Value ÷ 2ⁿ (integer division)

3. Arithmetic Shifts

Arithmetic shifts are similar to logical shifts but preserve the sign bit (MSB) when shifting right. This is essential for working with signed binary numbers using two's complement representation.

3.1 Arithmetic Left Shift

Arithmetic left shifts work identically to logical left shifts. The bit is shifted to the left, zeroes are added at the right, and the sign bit (if any) is discarded.

Example: 000110 << 2 = 011000
Same result as logical left shift!

3.2 Arithmetic Right Shift

In an arithmetic right shift, the bit is shifted to the right, but the most significant bit (sign bit) is copied to maintain the sign. The LSB is discarded.

📝 Arithmetic Right Shift Example

Original: 10110100 (binary) = -76 in two's complement

Shift right by 3: 10110100 >> 3

Result: 11110110 (binary) = -10 in two's complement

The sign bit (1) is preserved - it gets copied into the new leftmost positions!

Arithmetic Right Shift (>> 3) Original: 1 0 1 1 0 1 0 0 >> 3 Result: 1 1 1 1 0 1 1 0 Red = Sign bit copied Sign preserved!

3.3 Step-by-Step Arithmetic Right Shift

📝 Full Example: -24 >> 3 = -3

Original value: 11101000 = -24 (two's complement)

Each arithmetic right shift divides by 2, rounding towards negative infinity while preserving the sign bit.

Shift Type Left Shift Right Shift Used For
Logical Shift bits left, fill with 0 Shift bits right, fill with 0 Unsigned numbers
Arithmetic Same as logical left Shift right, preserve sign bit Signed numbers (two's complement)
🧠 Memory Trick: Sign Bit Preservation
💡 Exam Tip

When asked about arithmetic vs logical shifts, always mention: (1) Logical shifts are for unsigned numbers, (2) Arithmetic shifts preserve the sign bit for signed numbers, (3) Arithmetic left shift is identical to logical left shift!

4. Cyclic Shifts (Rotations)

Cyclic shifts (also called rotations) move bits in a circular fashion - bits that fall off one end are reintroduced at the other end. No bits are lost during a cyclic shift!

📖 Key Characteristics of Cyclic Shifts
📝 Cyclic Left Shift Example

Original: 10101111 (binary)

Cyclic left shift by 3:

Cyclic Left Shift (3 positions) 1 0 1 0 1 1 1 1 Original: 1 0 1 0 1 1 1 1 Amber = Wrapping bits No bits lost!

4.1 Cyclic Shift - Full Example

📝 Complete Cyclic Shift Example

Original: 10110001 = 177 (decimal)

Cyclic left by 3: 10001101 = 141 (decimal)

Cyclic right by 3: 00110010 = 50 (decimal)

Shift Type Left Shift Right Shift Application
Logical Shift bits left, fill with 0 Shift bits right, fill with 0 Unsigned numbers, raw bits
Arithmetic Same as logical left Shift right, preserve sign bit Signed numbers (two's complement)
Cyclic Rotate bits left (no loss) Rotate bits right (no loss) Encryption, checksums, bit rotation
🌟 Real-World Applications

Cyclic shifts are used in:

💡 Key Difference Summary

Logical/Arithmetic: Bits can be lost (fall off the end)

Cyclic: No bits lost - they wrap around!

5. Bit Masking

Bit masking is the process of using binary patterns (masks) to test, set, clear, or toggle specific bits without affecting the others. This is essential for device control and monitoring systems.

📖 What is a Mask?

A mask is a number used with logical operators (AND, OR, XOR, NOT) to identify, remove, or set a single bit or group of bits in an address or register.

5.1 Applications of Bit Masking

Operation Operator Purpose Example Use
Check Bit AND (&) Test if a specific bit is 1 Check if a sensor is active
Set Bit OR (|) Set specific bits to 1 Turn ON a device
Clear Bit AND with inverted mask Set specific bits to 0 Turn OFF a device
Toggle Bit XOR (^) Flip bits (0↔1) Switch device state
Bit Masking Concept Value: 1 0 1 1 Mask: 0 0 1 1 Result: 0 0 1 1 Only bits 1,0 visible AND
💡 Why Bit Masking?

Advantage: Speeds up processing and requires less memory. Instead of using full bytes for each flag, we can pack 8 flags into a single byte!

6. Bitwise AND Operation

The bitwise AND operation compares each bit position. It returns 1 only when BOTH bits are 1. This is used to test if specific bits are set.

📖 AND Truth Table
A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1
📝 Using AND to Check Bits

Scenario: Check if heater (Bit 5) and light (Bit 4) are both ON

Binary: 10111001

Mask: 00110000 (1s at positions 5 and 4)

Result: 00110000 (non-zero = both ON)

Bitwise AND: Check if Bits 5 & 4 are Set Binary: 1 0 1 1 1 0 0 1 ← Value Mask: 0 0 1 1 0 0 0 0 ← Mask AND: 0 0 1 1 0 0 0 0 ← Result Result ≠ 0 Both bits ARE set! Heater & Light: ON
⚠️ Key Use of AND

Use AND to isolate specific bits. If result is non-zero, the bits were set. If result is zero, the bits were not set. AND with 1 preserves the bit; AND with 0 always gives 0.

7. Bitwise OR and XOR Operations

7.1 Bitwise OR Operation

The bitwise OR operation returns 1 if EITHER or BOTH bits are 1. Used to SET specific bits to 1 without changing others.

A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1
📝 Using OR to Set Bits

Scenario: Turn ON alarm (Bit 6) and door lock (Bit 5)

Binary: 11001010

Mask: 01100000 (1s at positions 6 and 5)

Result: 11101010 (bits 6 and 5 now set to 1)

7.2 Bitwise XOR Operation

The bitwise XOR (exclusive OR) returns 1 only when bits are DIFFERENT. Used to TOGGLE specific bits (flip 0↔1).

A B A XOR B
0 0 0
0 1 1
1 0 1
1 1 0
📝 Using XOR to Toggle Bits

Scenario: Toggle fan (Bit 5) and heating (Bit 4)

Binary: 10101010

Mask: 00110000 (1s at positions 5 and 4)

Result: 10011010 (bits 5 and 4 flipped)

🧠 XOR Memory Trick

XOR = "eXclusive OR" = Only ONE can be 1

8. Assembly Language Bit Manipulation

Bit manipulation in assembly language uses specific instructions to work with individual bits in registers. ACC denotes Accumulator, IX denotes Index Register, # denotes a denary number, B denotes binary, & denotes hexadecimal.

8.1 Setting All Bits to Zero

📝 Clear All Bits Example
LDD 0034         ; Load byte into ACC from address 0034
AND #B00000000   ; AND with all zeros - clears all bits
STO 0034         ; Store altered byte back to address 0034
            

Any bit ANDed with 0 becomes 0!

8.2 Setting a Bit to 1

📝 Set Bit 2 to 1 Example
LDD 0034         ; Load byte into ACC from address 0034
OR #B00000100    ; OR with bit 2 set - sets bit 2 to 1
STO 0034         ; Store altered byte back to address 0034
            

Any bit ORed with 1 becomes 1. Other bits unchanged!

8.3 Toggling a Bit

📝 Toggle Bit 0 Example
LDD 0034         ; Load byte into ACC from address 0034
XOR #B00000001   ; XOR with bit 0 set - toggles bit 0
STO 0034         ; Store altered byte back to address 0034
            

XOR with 1 flips the bit. XOR with 0 leaves bit unchanged!

Assembly Bit Operations Summary AND Check / Clear bits AND 0 → Clear AND 1 → Keep OR Set bits to 1 OR 0 → Keep OR 1 → Set XOR Toggle bits XOR 0 → Keep XOR 1 → Flip

8.4 Isolating a Single Bit

📝 Keep Only Bit 1, Clear All Others
LDD 0034         ; Load byte into ACC from address 0034
AND #B00000010   ; AND with mask - keeps bit 1, clears all others
STO 0034         ; Store altered byte back to address 0034
            

Result: Bit 1 preserved, all other bits become 0. Useful for checking a single flag!

Example: Check if bit 1 is set
Original: 10110110
Mask: 00000010
Result: 00000010 (non-zero = bit 1 was set)
Operation Mask Pattern Effect
Clear all bits B00000000 All bits become 0
Set bit 2 B00000100 Only bit 2 becomes 1
Toggle bit 0 B00000001 Only bit 0 flips
Check bit 1 B00000010 All bits cleared except bit 1
💡 Exam Tip - Bit Position Numbering

Bits are numbered from right to left, starting at 0:

9. Glossary

📖 Key Terms
Term Definition
Binary Shift An operation that moves all bits in a binary number left or right by a certain number of positions
Logical Shift A shift where bits are moved and empty positions are filled with zeros; used for unsigned numbers
Arithmetic Shift A shift that preserves the sign bit (MSB) when shifting right; used for signed numbers
Cyclic Shift A rotation where bits that fall off one end are reintroduced at the other end; no bits lost
Bit Mask A binary pattern used with logical operators to test, set, clear, or toggle specific bits
MSB Most Significant Bit - the leftmost bit with highest positional value (bit 7 in an 8-bit number)
LSB Least Significant Bit - the rightmost bit with lowest positional value (bit 0)
AND Operation Bitwise operation that returns 1 only when both bits are 1; used to check/clear bits
OR Operation Bitwise operation that returns 1 if either or both bits are 1; used to set bits
XOR Operation Bitwise operation that returns 1 only when bits are different; used to toggle bits
Toggle To switch a bit from 0 to 1 or from 1 to 0 (flip the bit)
Flag A single bit used to indicate the status of a condition or device (on/off, true/false)
ACC Accumulator - a register used for arithmetic and logical operations

10. Exam-Style Questions

1. A binary number 00001110 is shifted left by 2 positions using a logical left shift. Show the result and explain the effect on the value. [4 marks]

Answer:

  • Original: 00001110 = 14 (decimal)
  • After left shift by 2: 00111000 = 56 (decimal)
  • The value has been multiplied by 4 (2² = 4)
  • Each left shift multiplies the value by 2
  • Two left shifts: 14 × 2 × 2 = 56 ✓

Additional: The two leftmost bits (00) were discarded, and two zeros were added on the right.

2. Explain the difference between a logical right shift and an arithmetic right shift. Give an example using an 8-bit binary number. [6 marks]

Answer:

  • Logical right shift: Fills the leftmost bit with 0 regardless of the original sign bit
  • Arithmetic right shift: Preserves the sign bit (MSB) by copying it into the new leftmost positions
  • Logical shift is used for unsigned numbers
  • Arithmetic shift is used for signed numbers (two's complement)
  • Example (logical): 11001000 >> 3 = 00011001 (fills with 0)
  • Example (arithmetic): 11001000 >> 3 = 11111001 (fills with 1, preserving sign)

Additional: Arithmetic right shift divides negative numbers correctly while preserving sign; logical right shift treats all numbers as unsigned.

3. An 8-bit register contains the binary value 10101111. Show the result after a cyclic left shift of 3 positions. [3 marks]

Answer:

  • Original: 10101111
  • The 3 leftmost bits (101) wrap around to the right
  • Result: 01111101
  • In cyclic shifts, no bits are lost - they rotate around

Additional: Original value = 175 decimal, Result = 125 decimal (values change but all bits preserved).

4. A control system uses an 8-bit register where each bit represents a different device. Bit 3 controls a heater. Write assembly language instructions to turn the heater ON. [4 marks]

Answer:

  • To set a bit to 1, we use the OR operation
  • The mask should have 1 at bit 3 position: B00001000
LDD address      ; Load byte into ACC
OR #B00001000    ; OR with mask to set bit 3
STO address      ; Store back to memory
                    

Additional: OR with 1 sets the bit to 1; OR with 0 leaves other bits unchanged.

5. The accumulator contains 10110110. Show the result after executing: XOR #B00001111 [3 marks]

Answer:

  • ACC: 10110110
  • Mask: 00001111
  • Result: 10111001
  • The lower 4 bits have been toggled (flipped)
  • XOR with 1 flips bits; XOR with 0 keeps bits unchanged

Additional: This technique is useful for toggling device states (ON↔OFF) in control systems.

10. Exam-Style Questions (Continued)

6. Explain how bit masking can be used to check if a specific bit is set. Use the example of checking if bit 5 is set in the binary value 11011010. [5 marks]

Answer:

  • To check a bit, use AND operation with a mask
  • The mask has 1 at the bit position to check: B00100000 (bit 5)
  • Value: 11011010
  • Mask: 00100000
  • Result: 00100000 (non-zero)
  • Since result is non-zero, bit 5 IS set
  • If result were zero, the bit was not set

Additional: In assembly: LDD address, AND #B00100000, then check if ACC = 0 (bit clear) or ≠ 0 (bit set).

7. Calculate the result of shifting the 8-bit binary number 11101000 (two's complement) arithmetically right by 2 positions. Show your working and state the decimal values before and after. [5 marks]

Answer:

  • Original: 11101000 = -24 (two's complement, negative)
  • Sign bit (MSB) = 1, so this is preserved
  • After shift 1: 11110100 = -12 (÷2)
  • After shift 2: 11111010 = -6 (÷2)
  • Each arithmetic right shift divides by 2
  • The sign bit (1) is copied into new positions

Additional: -24 ÷ 2 = -12, -12 ÷ 2 = -6. The arithmetic shift correctly preserves the negative sign.

8. A programmer wants to clear (set to 0) bits 4 and 5 in an 8-bit register while leaving all other bits unchanged. Write the assembly language instructions and explain the choice of operation. [5 marks]

Answer:

  • To clear bits, use AND operation
  • The mask should have 0s at bits 4 and 5, 1s elsewhere
  • Mask: B11001111
LDD address      ; Load byte into ACC
AND #B11001111   ; AND clears bits 4 and 5
STO address      ; Store result
                    
  • AND with 0 clears (sets to 0)
  • AND with 1 preserves the original bit
  • Bits 4 and 5 will become 0; all others unchanged
9. Compare and contrast logical shifts and cyclic shifts. Give one application for each. [6 marks]

Answer:

  • Similarities: Both move bits left or right by specified positions
  • Difference 1: Logical shifts add zeros; cyclic shifts wrap bits around
  • Difference 2: Logical shifts can lose bits (discarded); cyclic shifts preserve all bits
  • Difference 3: Logical shifts multiply/divide values; cyclic shifts rotate for encryption
  • Logical shift application: Fast multiplication/division by powers of 2
  • Cyclic shift application: Encryption algorithms, checksums, hash functions
  • In cyclic shifts, the same operation repeated 8 times returns to original (for 8-bit)
10. The accumulator contains 01100101. A programmer executes AND #B11110000 followed by OR #B00001010. Show the final result and explain what each instruction achieves. [6 marks]

Answer:

First operation (AND):

  • ACC: 01100101
  • Mask: 11110000
  • Result: 01100000
  • Lower 4 bits cleared (set to 0)

Second operation (OR):

  • ACC: 01100000
  • Mask: 00001010
  • Final result: 01101010
  • Bits 3 and 1 set to 1

Overall effect: Clear lower nibble, then set specific bits. This is useful for configuring device states where some bits must be cleared before setting new values.

11. Exam Success Tips (Part 1)

💡 Binary Shifts - Key Reminders
💡 Shift Types - When to Use Each
🧠 Memory Trick: Shift Direction
💡 Bit Masking Operations
💡 Bit Position Numbering

11. Exam Success Tips (Part 2)

⚠️ Common Mistakes to Avoid
❌ Typical Exam Errors
🧠 Assembly Language Patterns

11. Exam Success Tips (Part 3)

💡 Answer Structure Tips
🌟 Quick Reference Table
Topic Key Point
Logical Left Shift Bits move left, 0 fills right, MSB discarded → Multiply by 2
Logical Right Shift Bits move right, 0 fills left, LSB discarded → Divide by 2
Arithmetic Right Shift Same as logical but sign bit preserved
Cyclic Shift Bits rotate, nothing lost
AND Check bits (result ≠ 0 = set) or Clear bits (AND with 0)
OR Set bits to 1 (OR with 1)
XOR Toggle bits (XOR with 1)
📌 Final Exam Reminders

12. Key Takeaways

📌 Summary Points

Binary Shifts

Bit Masking

Assembly Language

Chapter 4.3 Overview SHIFTS • Logical (fill 0s) • Arithmetic (sign) • Cyclic (rotate) ← Multiply | Divide → OPERATIONS • AND = Check/Clear • OR = Set bits • XOR = Toggle Use with masks! APPLICATIONS • Fast math • Device control • Encryption Flags & sensors
🌟 Final Thought

Bit manipulation is one of the most fundamental and efficient operations in computing. Understanding how to manipulate individual bits gives you powerful control over hardware, enables optimization of algorithms, and forms the foundation for cryptography and data compression!