➕ Binary Addition and Subtraction
➕ Arithmetic Operations in Binary
Binary addition and subtraction are fundamental operations in digital systems. Understanding how these operations work is essential for comprehending computer arithmetic and digital logic.
➕ Binary Addition
Binary addition follows similar rules to decimal addition, but with only two digits (0 and 1):
📝 Basic Rules
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 = 0, carry 1 (equivalent to 2 in decimal, which is 10 in binary)
- 1 + 1 + 1 = 1, carry 1 (equivalent to 3 in decimal, which is 11 in binary)
🔢 Step-by-Step Process
- Add the rightmost bits
- If the sum is 2 or 3, record the remainder and carry the 1
- Continue left, including any carried digits
- After the leftmost addition, write any final carry as an extra bit
Example 1: Simple Addition
1 0 1 1 (11 in decimal)
+ 0 1 0 1 (5 in decimal)
---------
1 1 0 0 (16 in decimal)
Working right to left:
- 1 + 1 = 0, carry 1
- 1 + 0 + 1 (carry) = 0, carry 1
- 0 + 1 + 1 (carry) = 0, carry 1
- 1 + 0 + 1 (carry) = 0, carry 1
- Final carry: 1
- Result: 10000 (16 in decimal)
Example 2: Addition with Multiple Carries
1 1 1 1 (15 in decimal)
+ 0 1 1 1 (7 in decimal)
---------
1 0 1 1 0 (22 in decimal)
Working right to left:
- 1 + 1 = 0, carry 1
- 1 + 1 + 1 (carry) = 1, carry 1
- 1 + 1 + 1 (carry) = 1, carry 1
- 1 + 0 + 1 (carry) = 0, carry 1
- Final carry: 1
- Result: 10110 (22 in decimal)
➖ Binary Subtraction
There are several methods for binary subtraction. We'll cover the direct method and the complement method.
📝 Direct Method
Basic Rules
- 0 - 0 = 0
- 1 - 0 = 1
- 1 - 1 = 0
- 0 - 1 = 1, borrow 1 (equivalent to 2 - 1 = 1)
Example: Direct Subtraction
1 0 1 1 (11 in decimal)
- 0 1 0 1 (5 in decimal)
---------
0 1 1 0 (6 in decimal)
Working right to left:
- 1 - 1 = 0
- 1 - 0 = 1
- 0 - 1 = 1, borrow 1 (making the next digit 0)
- 0 - 0 = 0
- Result: 0110 (6 in decimal)
🔄 Two's Complement Method
The two's complement method is widely used in computers for representing negative numbers and performing subtraction.
To subtract B from A (A - B):
- Find the two's complement of B
- Add it to A
- Ignore any carry beyond the original number of bits
Finding Two's Complement
- Invert all bits (change 0 to 1 and 1 to 0)
- Add 1 to the result
Example: Subtraction Using Two's Complement
Subtract 5 (0101) from 11 (1011):
Step 1: Find two's complement of 5 (0101)
Invert bits: 1010
Add 1: + 1
-----
1011
Step 2: Add to 11 (1011)
1 0 1 1 (11 in decimal)
+ 1 0 1 1 (two's complement of 5)
---------
1 0 1 1 0
Step 3: Ignore the carry bit
0 1 1 0 (6 in decimal)
⚠️ Special Cases and Considerations
🔄 Overflow in Addition
Overflow occurs when the result of an operation is too large to be represented with the available bits:
1 1 1 1 (15 in decimal, maximum 4-bit value)
+ 0 0 0 1 (1 in decimal)
---------
1 0 0 0 0 (16 in decimal, requires 5 bits)
In fixed-width systems, the leftmost carry bit is lost, potentially causing errors.
➖ Negative Results in Subtraction
When subtracting a larger number from a smaller one using two's complement:
- The result will be in two's complement form
- The leftmost bit will be 1, indicating a negative number
To find the absolute value of a negative two's complement result, take its two's complement again.
💻 Practical Applications
Binary addition and subtraction are fundamental to:
- 🧮 Arithmetic operations in CPUs
- 🧠 Address calculations in memory
- 📊 Digital signal processing
- 📈 Implementing higher-level mathematical functions
- 🔍 Error detection and correction codes
Understanding these operations provides insight into how computers perform calculations at the most basic level.