By Dr. Sohel Rana
Chapter-1
.dld-wrap { font-family: ‘Segoe UI’, Tahoma, Geneva, Verdana, sans-serif; max-width: 1100px; margin: 0 auto; padding: 16px; background: #f0f4f8; color: #1a202c; box-sizing: border-box; } .dld-row { display: flex; flex-wrap: wrap; gap: 16px; margin: 14px 0; } .dld-col { flex: 1 1 260px; min-width: 0; box-sizing: border-box; } .dld-chapter { background: #fff; border-radius: 14px; padding: 20px; margin-bottom: 20px; box-shadow: 0 2px 12px rgba(0,0,0,0.07); } .dld-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin-top: 10px; } .dld-table { border-collapse: collapse; width: 100%; font-size: 0.82rem; text-align: center; } .dld-table th, .dld-table td { padding: 6px 8px; border: 1px solid #ccc; } .dld-mono { font-family: ‘Courier New’, monospace; } .dld-box { background: #fff; border-radius: 8px; padding: 12px; margin-top: 8px; font-family: ‘Courier New’, monospace; font-size: 0.88rem; color: #2d3436; line-height: 1.8; } h2.dld-h2 { margin: 0 0 14px 0; font-size: 1.4rem; } @media (max-width: 600px) { .dld-wrap { padding: 10px; } h2.dld-h2 { font-size: 1.15rem; } .dld-chapter { padding: 14px; } .dld-box { font-size: 0.8rem; } .dld-col { flex: 1 1 100%; } }🌟 Chapter 1 · Why Do We Need Number Systems?
Computers are machines made of tiny switches — only ON or OFF. Like a light switch! ON = 1, OFF = 0. This is the foundation of all computing. 💡
What YOU use daily. Digits 0–9. Your age, money, phone number!
What computers use. Only 0 & 1. On/Off switches inside chips!
Used in UNIX/Linux file permissions. Digits 0–7.
Colors in web/apps! #FF5733 is orange. Digits 0–9 & A–F.
Think of number systems like languages. Humans speak Decimal, computers speak Binary, programmers shorthand with Hex — all saying the same thing in different ways! 🗣️
🏠 Chapter 2 · Decimal Number System (Base 10)
The system you already know! We have 10 fingers, so humans naturally use 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. After 9 we carry over. 🤲
Each digit has a position weight = Baseposition
| Number | Thousands 10³ | Hundreds 10² | Tens 10¹ | Units 10⁰ | Calculation |
|---|---|---|---|---|---|
| 1234 | 1 | 2 | 3 | 4 | 1×1000+2×100+3×10+4×1=1234 |
| 305 | 0 | 3 | 0 | 5 | 3×100+0×10+5×1=305 |
💡 Same logic applies to Binary, Octal, and Hex — just change the base!
💻 Chapter 3 · Binary Number System (Base 2)
Only two digits: 0 and 1. Like a coin — only Heads or Tails! 🪙
Divide by 2, read remainders BOTTOM to TOP
13 ÷ 2 = 6 rem 1 ← LSB
6 ÷ 2 = 3 rem 0
3 ÷ 2 = 1 rem 1
1 ÷ 2 = 0 rem 1 ← MSB
Read ↑ bottom to top: 1101 ✅
Multiply each bit by 2position, then sum
1×2³ = 1×8 = 8
1×2² = 1×4 = 4
0×2¹ = 0×2 = 0
1×2⁰ = 1×1 = 1
8+4+0+1 = 13 ✅
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| 2ⁿ | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
🧠 Trick: “1-2-4-8-16-32-64-128” — just double each time going left!
0 + 0 = 0 (no carry) 😊
0 + 1 = 1 (no carry) 😊
1 + 0 = 1 (no carry) 😊
1 + 1 = 0, carry 1 🚀
1+1+1 = 1, carry 1 🚀
carry: 1 1 1
1 0 1 1
+ 0 1 1 0
1 0 0 0 1 = 10001 = decimal 17 ✅
0 − 0 = 0 😊
1 − 0 = 1 😊
1 − 1 = 0 😊
0 − 1 = 1, borrow 1 from left 😬
Borrow 1 from next column = borrow 2 in value!
1 1 0 1
− 0 1 0 1
1 0 0 0 = 1000 = decimal 8 ✅
Verify: 13 − 5 = 8 ✔️
- ❌ Forgetting the carry in addition — always check the column to the left!
- ❌ Reading position from LEFT instead of RIGHT (position 0 is RIGHTMOST)
- ❌ Confusing MSB (leftmost) with LSB (rightmost)
- ✅ Write positions 0,1,2,3… from RIGHT to LEFT under each bit
🎱 Chapter 4 · Octal Number System (Base 8)
Uses digits 0 to 7 only. After 7, write 10 (= 8 in decimal). Think of an octopus 🐙 with 8 arms!
📌 Real Use: Linux file permissions! chmod 755 — those numbers are octal!
Divide by 8, read remainders bottom to top
100 ÷ 8 = 12 rem 4
12 ÷ 8 = 1 rem 4
1 ÷ 8 = 0 rem 1
Read ↑: 144₈ ✅
Multiply each digit by 8position
1×8² = 1×64 = 64
4×8¹ = 4×8 = 32
4×8⁰ = 4×1 = 4
64+32+4 = 100 ✅
Every Octal digit = exactly 3 binary bits. Group from right!
Octal: 7 5 3 → Binary: 111 101 011
| Octal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Binary | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
🎨 Chapter 5 · Hexadecimal (Base 16)
Uses 16 symbols: digits 0–9 and letters A–F. A=10, B=11, C=12, D=13, E=14, F=15 🔤
📌 Daily Life: Web colors! #FF5733 = Orange #1ABC9C = Teal
| Dec | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
| Bin | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
255 ÷ 16 = 15 rem F
15 ÷ 16 = 0 rem F
Result: FF₁₆ ✅
Max brightness in RGB = FF!
1 Hex digit = exactly 4 binary bits
→ A F → AF₁₆ ✅
Hex: 3B
→ 0011 1011 → 00111011₂ ✅
🔢 Chapter 6 · BCD — Binary Coded Decimal
BCD encodes each decimal digit separately into 4 bits. Like giving each digit its own little box! 📦
📌 Real Use: Digital clocks ⏰, calculators 🧮, ATM displays 💳
| Digit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| BCD | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 |
0100Digit 7 →
0111BCD of 47 = 0100 0111
0010111147 in BCD =
0100 0111Different! BCD uses more bits but easier for displays!
🔄 Chapter 7 · Gray Code
Only ONE bit changes between consecutive numbers! Prevents errors when multiple bits change. 🎯
📌 Analogy: Like a stove dial 🔥 — smooth, one-step changes. Used in rotary encoders, K-maps!
| Decimal | Binary | Gray Code | Bits Changed |
|---|---|---|---|
| 0 | 000 | 000 | — |
| 1 | 001 | 001 | 1 ✅ |
| 2 | 010 | 011 | 1 ✅ |
| 3 | 011 | 010 | 1 ✅ |
| 4 | 100 | 110 | 1 ✅ |
| 5 | 101 | 111 | 1 ✅ |
| 6 | 110 | 101 | 1 ✅ |
| 7 | 111 | 100 | 1 ✅ |
MSB stays same. Each Gray bit = XOR of current & previous binary bit.
G₃ = B₃ = 1
G₂ = B₃⊕B₂ = 1⊕0 = 1
G₁ = B₂⊕B₁ = 0⊕1 = 1
G₀ = B₁⊕B₀ = 1⊕1 = 0
Gray = 1110 ✅
MSB stays same. Each Binary bit = XOR of previous binary & current gray bit.
B₃ = G₃ = 1
B₂ = B₃⊕G₂ = 1⊕1 = 0
B₁ = B₂⊕G₁ = 0⊕1 = 1
B₀ = B₁⊕G₀ = 1⊕0 = 1
Binary = 1011 ✅
🔁 Chapter 8 · 1’s & 2’s Complement
These are how computers handle negative numbers and perform subtraction! 🧮
Simply flip every bit! 0→1, 1→0
1’s comp: 11110101
⚠️ Problem: Two zeros!
00000000 = +0
11111111 = −0 (awkward!)
Step 1: Find 1’s complement Step 2: Add 1
1’s comp = 11110101
Add 1 →
−10 = 11110110
Subtraction becomes addition: 5 − 3 = 5 + (−3)
−3 = 11111101 (2’s comp of 3)
= 1 00000010 → drop overflow → 00000010 = +2 ✅
➕➖ Chapter 9 · Signed & Unsigned Numbers
Like a Cricket Score — can never be negative! All bits used for value.
- Only 0 and above
- 8-bit range: 0 to 255
- 16-bit range: 0 to 65535
- n-bit: 0 to 2ⁿ−1
00000000₂ = 0 (min)
📌 Used for: pixel colors, memory addresses, age
Like a Bank Balance — can go negative! MSB is the sign bit.
- MSB = 0 → Positive ✅
- MSB = 1 → Negative ❌
- 8-bit range: −128 to +127
- n-bit: −2ⁿ⁻¹ to 2ⁿ⁻¹−1
10000000₂ = −128 (min)
📌 Used for: temperature, bank balance, scores
| Feature | Unsigned | Signed SM | 1’s Comp | 2’s Comp ⭐ |
|---|---|---|---|---|
| Negatives? | ❌ | ✅ | ✅ | ✅ |
| Max 8-bit | +255 | +127 | +127 | +127 |
| Min 8-bit | 0 | −127 | −127 | −128 |
| Zeros | One | Two | Two | One ⭐ |
| Used in | Counters | Old | Some HW | All CPUs! |
Delhi summer ☀️ = +45°C → positive, MSB=0
Ladakh winter ❄️ = −20°C → negative, MSB=1 (2’s complement)
The sign bit tells the computer: warm day or freezing cold! 😄
💥 Chapter 10 · Overflow!
Overflow = result too large for the bit count. Like pouring 300mL into a 250mL cup — it spills! ☕
+ 00000001 ( 1)
= 100000000 → only 8 bits kept = 00000000 (0) ❌
Wrapped around to 0!
+ 00000001 ( +1)
= 10000000 (−128) ❌ +127+1 should be +128
but MSB flipped → looks negative!
If carry INTO MSB ≠ carry OUT OF MSB → Overflow occurred! 🚨
📊 Chapter 11 · Master Reference Table (0–15)
All systems at a glance — bookmark this! 🔖
| Dec | Binary | Octal | Hex | BCD | Gray |
|---|---|---|---|---|---|
| 0 | 0000 | 0 | 0 | 0000 | 0000 |
| 1 | 0001 | 1 | 1 | 0001 | 0001 |
| 2 | 0010 | 2 | 2 | 0010 | 0011 |
| 3 | 0011 | 3 | 3 | 0011 | 0010 |
| 4 | 0100 | 4 | 4 | 0100 | 0110 |
| 5 | 0101 | 5 | 5 | 0101 | 0111 |
| 6 | 0110 | 6 | 6 | 0110 | 0101 |
| 7 | 0111 | 7 | 7 | 0111 | 0100 |
| 8 | 1000 | 10 | 8 | 1000 | 1100 |
| 9 | 1001 | 11 | 9 | 1001 | 1101 |
| 10 | 1010 | 12 | A | ❌ | 1111 |
| 11 | 1011 | 13 | B | ❌ | 1110 |
| 12 | 1100 | 14 | C | ❌ | 1010 |
| 13 | 1101 | 15 | D | ❌ | 1011 |
| 14 | 1110 | 16 | E | ❌ | 1001 |
| 15 | 1111 | 17 | F | ❌ | 1000 |
⚠️ Red rows (10–15): Invalid in BCD — BCD only covers digits 0–9!
🧠 Chapter 12 · Tricks, Tips & Common Mistakes
- 🔢 Powers of 2: “1-2-4-8-16-32-64-128” — double each time!
- 🎱 Octal = Octopus = 8 arms = 8 digits (0–7)
- 🎨 Hex colors: R,G,B each = 2 hex digits (00–FF)
- 🔄 Binary ↔ Octal: group by 3 bits
- 🔄 Binary ↔ Hex: group by 4 bits
- 📝 2’s complement: “flip then add 1”
- 📌 MSB = leftmost = Most Significant (or sign bit)
- 📌 LSB = rightmost = Least Significant
- ❌ Mixing up BCD and pure binary
- ❌ Forgetting BCD codes 1010–1111 are INVALID
- ❌ Reading binary position LEFT to RIGHT (0 is RIGHTMOST!)
- ❌ Forgetting carry in binary addition
- ❌ Confusing 1’s complement with 2’s complement
- ❌ Not adding leading zeros before grouping bits
- ❌ Treating a 2’s complement negative as positive
- ❌ Ignoring overflow when adding same-sign numbers
| Convert | Method | Shortcut? |
|---|---|---|
| Dec → Binary | Divide by 2, remainders ↑ | None |
| Binary → Dec | Sum of bit × 2ⁿ | None |
| Binary → Octal | Group 3 bits from right | ⚡ YES |
| Octal → Binary | Each digit → 3 bits | ⚡ YES |
| Binary → Hex | Group 4 bits from right | ⚡ YES |
| Hex → Binary | Each digit → 4 bits | ⚡ YES |
| Dec → BCD | Each decimal digit → 4-bit | ⚡ YES |
| Binary → Gray | MSB same; XOR each with prev | Formula |
| Dec → 2’s Comp | Convert → flip → add 1 | Or: flip after first 1 from right |
From RIGHT: keep all bits up to and including the first 1, then flip all remaining bits!
Keep →→→→→→ 1 0 0 (first 1 + trailing zeros)
Flip rest: 1 0 1 0 0 1 1 0 0
Result: 101001100 ✅ Faster than flip+add1!
🌍 Chapter 13 · Real World Applications
#FF5733 → Red=FF(255), Green=57(87), Blue=33(51). Every web color = 3 hex bytes!
12:35 stored as BCD: 0001 0010 : 0011 0101. Each digit = 4-bit code!
1 KB = 2¹⁰ = 1024 bytes. 1 GB = 2³⁰. Always powers of 2!
192.168.1.1 — each part is an 8-bit unsigned number. Max is 255!
−20°C stored using signed 8-bit (2’s complement). Negative = MSB is 1!
RAM shown as 0xFFFF — hex is compact vs 1111111111111111 binary!
✏️ Quick Practice Problems
- 42₁₀ → Binary = ____?
- 11001₂ → Decimal = ____?
- 75₁₀ → Octal = ____?
- 1011 0101₂ → Hex = ____?
- 39₁₀ → BCD = ____?
- 1010₂ → Gray Code = ____?
- 2’s comp of 00010011 = ____?
- 101010
- 25
- 113₈
- B5₁₆
- 0011 1001
- 1111
- 11101101
🧮 The Complete Number Systems Encyclopedia
From Zero to Hero: Binary, Hex, Signed Math, and Digital Codes
🌟 1. Introduction: Why Bases?
Computers are like a row of light switches—either ON (1) or OFF (0). This is why we use Base-2.
- Base-10 (Decimal): 0-9 (Our fingers! 🖐️)
- Base-2 (Binary): 0, 1 (The PC’s soul 💻)
- Base-8 (Octal): 0-7 (Shorthand for 3 bits)
- Base-16 (Hex): 0-F (Shorthand for 4 bits – used in Color Codes like #FFFFFF!)
🔄 2. Conversion Secrets
| Type | Method |
|---|---|
| Dec ➡️ Any | Divide & Conquer: Divide by base, track remainders bottom-up. |
| Any ➡️ Dec | Weight Lifting: Multiply digits by $Base^{position}$. |
| Bin ➡️ Hex | The “4-Pack”: Group bits by 4. Use 8-4-2-1 rule. |
| Oct ➡️ Bin | The “Trio”: Each Octal digit becomes 3 bits. |
➕ 3. Binary Math Rules
0+0 = 0
0+1 = 1
1+1 = 10
1+1+1 = 11
0-0 = 0
1-0 = 1
1-1 = 0
10-1 = 1 (Borrow)
💡 Trick: In subtraction, if you borrow, the ‘0’ becomes ‘2’ (in decimal thinking) because $Base=2$.
⚖️ 4. Signed Numbers (Negatives)
MSB (Leftmost bit) is the Boss: 0 = Positive (+), 1 = Negative (-).
1. Positive 5:
000001012. 1’s Comp (Flip):
111110103. 2’s Comp (Add 1):
11111011 ✅
🔡 5. Specialized Codes
• BCD (Binary Coded Decimal): For digital clocks! ⏰ Each digit gets 4 bits.
Ex: 25 ➡️ 0010 0101.
• Gray Code: The “Unit Distance” code. Only 1 bit flips at a time. Used to prevent glitches in sensors! 🛰️
• Excess-3: BCD + 0011 (3). Helps in subtraction.
🛑 Avoid These Traps!
- ❌ Mistake: Thinking 1010 is BCD for “10”.
✅ Fact: BCD only goes up to 9 (1001). 10 is 0001 0000. - ❌ Mistake: Forgetting to carry in Binary Addition.
- ❌ Mistake: Using 2’s Comp on positive numbers.
✅ Fact: 2’s Complement is only for NEGATIVE representation.
📊 Decimal to Binary/Hex/Octal Quick Map
| Decimal | Binary (4-bit) | Octal | Hexadecimal | BCD |
|---|---|---|---|---|
| 0 | 0000 | 0 | 0 | 0000 |
| 5 | 0101 | 5 | 5 | 0101 |
| 9 | 1001 | 11 | 9 | 1001 |
| 10 | 1010 | 12 | A | 0001 0000 |
| 15 | 1111 | 17 | F | 0001 0101 |
💡 Remember: Computers aren’t smart, they’re just fast! Master the numbers, master the machine. 💻✨
DLD Comprehensive Series | Part 01: Number Systems
1$!)
📊 The Master Cheat Sheet (0-15)
This table covers everything you will ever need.
| Decimal (Base 10) | Binary (Base 2) | Octal (Base 8) | Hexadecimal (Base 16) |
| 0 | 0000 | 0 | 0 |
| 1 | 0001 | 1 | 1 |
| 2 | 0010 | 2 | 2 |
| 3 | 0011 | 3 | 3 |
| 4 | 0100 | 4 | 4 |
| 5 | 0101 | 5 | 5 |
| 6 | 0110 | 6 | 6 |
| 7 | 0111 | 7 | 7 |
| 8 | 1000 | 10 | 8 |
| 9 | 1001 | 11 | 9 |
| 10 | 1010 | 12 | A |
| 11 | 1011 | 13 | B |
| 12 | 1100 | 14 | C |
| 13 | 1101 | 15 | D |
| 14 | 1110 | 16 | E |
| 15 | 1111 | 17 | F |
🧠 How to Remember (Without Memorizing!)
You only need to remember two magic codes:
1. The “4-2-1” Trick (For Octal) 🐙
Octal digits go from 0 to 7. Notice that $4+2+1 = 7$.
This is all you need to find any Octal number.
- Question: What is 4 in Octal?
- Look at your code: 4 – 2 – 1
- Do you need the “4”? YES (Put a 1)
- Do you need the “2”? NO (Put a 0)
- Do you need the “1”? NO (Put a 0)
- Result:
100in binary is 4 in Octal.
- Question: What is 6 in Octal?
- How do you make 6? You need 4 + 2.
- 4 (Yes) – 2 (Yes) – 1 (No)
- Result:
110
2. The “8-4-2-1” Trick (For Hexadecimal) 🔶
Hex goes up to 15. Notice that $8+4+2+1 = 15$.
This is the code for Hex.
- Question: What is C (12) in Binary?
- How do you make 12? 8 + 4.
- 8 (Yes) – 4 (Yes) – 2 (No) – 1 (No)
- Result:
1100
- Question: What is F (15) in Binary?
- How do you make 15? 8 + 4 + 2 + 1.
- Result:
1111(All switches ON!)
✍️ Exam Hack: Build the Table in 10 Seconds
If you sit in an exam, draw this immediately on rough paper. You don’t need to calculate anything. Just follow this pattern:
- Column 1 (The “1s” place): Alternating
0, 1, 0, 1, 0, 1... - Column 2 (The “2s” place): Alternating
00, 11, 00, 11... - Column 3 (The “4s” place): Alternating
0000, 1111... - Column 4 (The “8s” place): Alternating
00000000, 11111111...
Try it:
Write 0-1-0-1 down the page.
Next to it, write 00-11-00-11.
Next to that, 0000-1111.
Boom! You just wrote the entire binary table without doing a single math calculation. 😎
🛑 Common Confusion Check
User asked: “What is 4 equivalent in octal?”
- In Decimal: It is 4.
- In Octal: It is also 4.
- In Hex: It is also 4.
Why? Because all number systems are the SAME until you run out of digits!
- Binary runs out at 1 (so 2 becomes
10). - Octal runs out at 7 (so 8 becomes
10). - Decimal runs out at 9 (so 10 becomes
10). - Hex runs out at F (so 16 becomes
10).
So, 0, 1, 2, 3, 4, 5, 6, 7 are the identical in Octal, Decimal, and Hex! The confusion only starts at number 8.
Hi ! Think of me as your study partner. I know Digital Logic Design (DLD) can feel like a maze of 1s and 0s, but let’s break down Signed and Unsigned numbers as if we’re talking about a simple bank ledger or a cricket score.
Here is a simplified guide you can copy and paste into your notes.
🔢 Understanding Signed vs. Unsigned Numbers
In the world of computers, everything is stored in “boxes” called bits. Usually, we use 8 bits (1 Byte) to represent a number.
1. Unsigned Numbers (The “Positive Only” Club)
Unsigned numbers are like a tape measure. They start at zero and only go up. There is no concept of “negative” here.
- Rule: All bits are used to represent the value of the number.
- Range (for 8 bits): $0$ to $2^8 – 1$ (which is 0 to 255).
- Example: * Binary
00001010= Decimal10- Binary
11111111= Decimal255
- Binary
Real-life Application: Think of your Age or the Number of runs a batsman scores. You can’t have -20 runs or be -5 years old!
2. Signed Numbers (The “Plus and Minus” Club)
Signed numbers are like a Bank Statement. You can have money in the bank (Positive), or you can be in debt (Negative).
To show if a number is positive or negative, we reserve the left-most bit (the Most Significant Bit or MSB) as a “Sign Post.”
- The Sign Bit Rule:
- If the first bit is 0 $\rightarrow$ The number is Positive (+).
- If the first bit is 1 $\rightarrow$ The number is Negative (-).
How do we write Negative numbers?
In DLD, we don’t just put a minus sign. We use 2’s Complement.
- Start with the positive binary.
- Flip all bits (0 becomes 1, 1 becomes 0).
- Add 1 to the result.
- Example (using 8 bits):
+5is00000101(Starts with 0)-5is11111011(Starts with 1)
Real-life Application: Temperature. It can be $+40^\circ\text{C}$ in Delhi or $-10^\circ\text{C}$ in Leh. We need a sign bit to tell the difference!
📊 Quick Comparison Table
| Feature | Unsigned Numbers | Signed Numbers |
| Range (8-bit) | 0 to 255 | -128 to +127 |
| Sign Bit | No (All bits are value) | Yes (Left-most bit) |
| Negatives? | Not possible | Possible |
| Best For | Counting items, Memory addresses | Calculations, Temperature, Finance |
💡 A Simple Way to Remember
- Unsigned: Think of a Whole Number ($0, 1, 2, 3…$). It’s simple and straightforward.
- Signed: Think of a Direction. If you walk forward, it’s (+). If you walk backward, it’s (-). The first bit is just a “direction arrow.”
🧠 Check your understanding:
If you see the binary number 10000010:
- In Unsigned, it is a large positive number (130).
- In Signed, it is a Negative number because it starts with 1.
// — DLD CHAPTER: 2’S COMPLEMENT MASTERY — let dld_advanced_pg = `
🔢 2’s Complement Deep Dive
Hey ${name}! Let’s master the math of the CPU. 2’s Complement is the industry standard because it makes addition and subtraction identical!
To convert a positive number (X) to its negative counterpart (-X):
1. Binary: Write the magnitude in 8 bits.
2. Flip: Change all 0s to 1s and 1s to 0s (1’s Comp).
3. Add 1: Add binary 1 to the LSB.
Tip: The MSB (leftmost bit) is your sign! 1 = Negative.
A: It eliminates “Negative Zero” and simplifies hardware adder circuits.
Q2: Range of 8-bit signed?
A: -128 to +127.
Q3: What is 0000 0000 in 2’s Comp?
A: Still 0. Adding 1 to 1111 1111 causes a carry-out that is ignored.
Q4: Convert -1 to binary (8-bit).
A: 1111 1111.
Q5: Is 1000 0000 positive?
A: No, it’s the “weird” case: -128.
A: 0000 1101 (Positives don’t change!)
Q7: Convert -5 to 2’s Comp.
A: 00000101 → 11111010 + 1 = 1111 1011
Q8: What is 1111 1110 in decimal?
A: Flip & add 1 back → 00000001 + 1 = 2. So, -2.
Q9: What happens during Overflow?
A: Two positives add to make a negative (or vice versa).
Q10: Calculate 5 – 3 using 2’s Comp.
A: 5 + (-3) → 0101 + 1101 = (1)0010 = 2.
Scan bits from Right to Left. Keep everything the same until you hit the first ‘1’. Keep that ‘1’, then flip everything to the left of it!
4 is 0000 0100.
First ‘1’ is at 2^2. Keep ‘100’, flip rest: 1111 1100.
⚡ Flashback Revision: The “Sign” Problem
- Computers only know 0 and 1. They don’t have a “+” or “-” key.
- So, we steal the first bit (MSB) to act as the sign.
- 0 = Positive (+) 😄
- 1 = Negative (-) 😠
| Feature | Unsigned Numbers | Signed Numbers |
| Meaning | Only positive numbers (magnitude only). | Can be Positive (+) or Negative (-). |
| The MSB (First Bit) | It is part of the value (e.g., 128). | It is the Sign Bit (0 is +, 1 is -). |
| Range (for 8 bits) | 0 to 255 (Huge positive range). | -128 to +127 (Split range). |
| Example (1111) | $8+4+2+1 = \mathbf{15}$ | In Signed 2’s comp, this is -1. |
| Use Case | Counting apples, memory addresses. | Temperature, Bank balance (debt). |
Q1(a) Represent the decimal number 232 in Binary, Gray Code, Octal, BCD, and Excess-3.
1. Decimal to Binary
Target Number: 232
We find the powers of 2 that add up to 232:
- 128 + 64 + 32 + 8 = 232
So, we place a “1” at those positions and “0” at the others:
Binary Answer: 1110 1000
2. Binary to Gray Code
Rule: Copy the first bit, then XOR (add without carry) the neighbors.
Binary: 1110 1000
- 1 (Copy first) -> 1
- 1 + 1 = 0
- 1 + 1 = 0
- 1 + 0 = 1
- 0 + 1 = 1
- 1 + 0 = 1
- 0 + 0 = 0
- 0 + 0 = 0
Gray Code Answer: 1001 1100
3. Binary to Octal
Rule: Group binary bits in sets of 3 starting from the right.
Binary: 011 | 101 | 000
- 011 = 3
- 101 = 5
- 000 = 0
Octal Answer: 350 (Base 8)
4. Decimal to BCD (Binary Coded Decimal)
Rule: Convert each decimal digit separately into 4-bit binary.
Digits: 2 | 3 | 2
- 2 = 0010
- 3 = 0011
- 2 = 0010
BCD Answer: 0010 0011 0010
5. Decimal to Excess-3
Rule: Add 3 to each decimal digit, then convert to binary.
Digits: 2 | 3 | 2
- First Digit (2): 2 + 3 = 5 -> 0101
- Second Digit (3): 3 + 3 = 6 -> 0110
- Third Digit (2): 2 + 3 = 5 -> 0101
Excess-3 Answer: 0101 0110 0101
Q1(b) Difference between Signed and Unsigned Binary Numbers
Unsigned Numbers:
- Definition: These numbers can only be positive.
- MSB (Most Significant Bit): The first bit is used for the value (magnitude), just like any other bit.
- Range: Stores larger positive numbers (e.g., 0 to 255).
- Example: 1111 1111 = 255.
Signed Numbers:
- Definition: These numbers can be positive or negative.
- MSB (Most Significant Bit): The first bit is the “Sign Bit”.
- 0 means Positive (+)
- 1 means Negative (-)
- Range: The range is split between positive and negative (e.g., -128 to +127).
- Example: 1111 1111 usually represents -1 in Two’s Complement.
Q1(c) How to perform Addition and Subtraction using 2’s Complement
1. Addition
Addition in 2’s complement is straightforward. You simply add the two binary numbers together. If there is a carry out of the final bit, it is discarded.
Example: 5 + 2
0101 (5)
- 0010 (2)
0111 (Answer is 7)
2. Subtraction
Computers perform subtraction (A – B) by adding the negative version of the second number (A + (-B)).
Step 1: Find the 2’s Complement of the number to be subtracted (B).
- Example: 7 – 5
- Binary for 5 is 0101.
- Invert digits (1s Complement): 1010
- Add 1: 1011 (This is -5)
Step 2: Add the first number (A) to the 2’s Complement of B.
0111 (7)
- 1011 (-5)
10010
Step 3: Discard the final carry.
Since we are working with 4 bits, we ignore the extra ‘1’ on the far left.
Final Result: 0010 (Which is equal to decimal 2)



