Binary, BCD, Octal, and Hexadecimal Arithmetic
Why Do Computers Need Special Arithmetic?
Inside the CPU, the Arithmetic Logic Unit (ALU) performs millions of calculations every second.
- Computer chips cannot understand signs like
+or-. - They only understand high and low electrical voltages (1s and 0s).
- Designing separate electronic circuits for addition and subtraction is costly and wastes chip area.
Instead, computer engineers use a clever trick called 2’s complement. With 2’s complement, the same adder circuit can perform both addition and subtraction!
💡 Real-Life Indian Student Analogy:
Think of an old mechanical bike odometer.
If it reads00km and you roll the wheel backward by 1 km, it shows99km!
Complement arithmetic works the exact same way. It turns subtraction into addition by rolling numbers around a circle.
1. Binary Arithmetic Basics
A. Binary Addition Rules
There are only 4 simple rules for adding binary numbers:
0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 0(with a Carry of 1, because 2 in binary is10₂)1 + 1 + 1(when there is a previous carry) = 1 (with a Carry of 1, because 3 in binary is11₂)
Example: Add (1011)₂ and (1101)₂:
Carry: 1 1 1
1 0 1 1 (= 11 in decimal)
+ 1 1 0 1 (= 13 in decimal)
---------
1 1 0 0 0 (= 24 in decimal)
B. How Computers Store Negative Numbers
How does computer memory know if a number is positive (+5) or negative (-5)?
-
Sign-Magnitude Form:
- The leftmost bit is the Sign Bit (
0= Positive,1= Negative). - The remaining bits show the number value.
- Example (8-bit):
+5=00000101,-5=10000101. - Problem: It gives two different codes for zero (
+0and-0), which confuses the CPU.
- The leftmost bit is the Sign Bit (
-
1’s Complement:
- Simply flip every bit: turn all
0s to1s, and1s to0s. +5=00000101→ 1’s complement is11111010(represents-5).
- Simply flip every bit: turn all
-
2’s Complement (Used in All Modern Computers):
- Formula: 2’s Complement = 1’s Complement + 1
- Let’s find
-5:+5=00000101- 1’s complement =
11111010 - Add 1:
11111010 + 1 = 11111011(This is-5).
- Why is it best? It has only one zero (
00000000) and standard addition hardware works directly.
2. Subtraction using 2’s Complement (A - B)
To calculate A - B, the computer actually calculates:
A + (2’s complement of B)
The 3 Easy Steps:
- Find the 2’s complement of the number you are subtracting (B).
- Add this value to the first number (A).
- Check the extra carry at the left:
- If Carry = 1: Discard it! The answer is positive and ready.
- If Carry = 0: The answer is negative. Take 2’s complement of the result and put a minus sign (
-) in front.
Solved Example: Calculate 28 - 14 using 8-bit 2’s Complement
- A = 28 in binary =
00011100 - B = 14 in binary =
00001110 - Find 2’s complement of 14:
- 1’s complement:
11110001 - Add 1:
11110001 + 1 = 11110010
- 1’s complement:
- Now add A and (2’s complement of B):
00011100 (28)
+ 11110010 (-14)
----------
1 00001110
↑
End Carry = 1 (Discard it!)
- Final answer:
00001110₂= +14 in decimal (Correct!)
3. BCD (Binary Coded Decimal) Arithmetic
In BCD (8421 code), each decimal digit from 0 to 9 is written separately as a 4-bit group.
- Example: Decimal
25is written as0010 0101. - A 4-bit group can count up to 15 (
1111₂). - But decimal digits stop at 9 (
1001₂). - Therefore, patterns from 10 to 15 (1010 to 1111) are INVALID BCD.
The Golden Rule of BCD Addition (+6 Rule):
When adding two BCD numbers:
- Add them using standard binary addition.
- If any 4-bit group is greater than 9 OR produces a carry out:
- Add 6 (
0110₂) to that group! - Why 6? Because
16 - 10 = 6. Adding 6 skips over the 6 invalid codes and fixes the decimal carry. - Pass any resulting carry to the next 4-bit group on the left.
- Add 6 (
Solved Example: Add 35 + 48 in BCD
- 35 =
0011 0101 - 48 =
0100 1000
0011 0101 (35)
+ 0100 1000 (48)
----------------
0111 1101 (Right group is 1101 = 13, which is > 9: INVALID!)
+ 0000 0110 (Add +6 correction to fix it)
----------------
1000 0011
8 3 = 83 in decimal! (Correct!)
4. Octal and Hexadecimal Arithmetic
Octal Arithmetic (Base 8)
- Uses digits 0 to 7.
- Addition: If the column sum is 8 or more, subtract 8 and carry 1 to the left.
- Subtraction: If you borrow from the left column, you borrow an amount of 8 (not 10).
Example: Add (57)₈ + (34)₈:
- Right Column:
7 + 4 = 11. Since 11 ≥ 8:11 - 8 = 3with carry 1. - Left Column:
1 (carry) + 5 + 3 = 9. Since 9 ≥ 8:9 - 8 = 1with carry 1. - Result = (113)₈
Hexadecimal Arithmetic (Base 16)
- Uses digits 0 to 9 and letters A to F.
- Addition: If the column sum is 16 or more, subtract 16 and carry 1 to the left.
- Subtraction: If you borrow from the next column, you borrow 16.
Example: Add (4B)₁₆ + (7A)₁₆:
- Right Column:
B + A = 11 + 10 = 21.- Since 21 ≥ 16:
21 - 16 = 5with carry 1.
- Since 21 ≥ 16:
- Left Column:
1 (carry) + 4 + 7 = 12 = C. - Result = (C5)₁₆
💡 DAVV Exam Preparation Tips
5-Mark Question: “Perform the subtraction 35 - 50 using 2’s complement 8-bit arithmetic.”
- Convert both numbers to 8-bit binary:
- 35 =
00100011- 50 =
00110010- Take 2’s complement of 50:
11001101 + 1 = 11001110.- Add:
00100011 + 11001110 = 11110001.- State clearly: End carry = 0, so the result is negative.
- Take 2’s complement of answer:
00001110 + 1 = 00001111= 15.- Final result is -15.
10-Mark Question: “Explain BCD addition rules. Why is the correction factor of 6 (0110) added? Illustrate with two examples.”
- Define BCD (8421 code) where each digit 0-9 takes 4 bits.
- Explain why +6 is needed: A 4-bit adder counts to 16, but decimal base is 10. The difference is
16 - 10 = 6invalid codes.- Give two clear examples:
- Example 1: Sum ≤ 9 (no correction needed, e.g., 23 + 14 = 37).
- Example 2: Sum > 9 (correction with +0110, e.g., 35 + 48 = 83).