Discrete Mathematics · Discrete Mathematics
Binary Arithmetic: Addition and Subtraction in Base 2
Explanation
Intuition: Why Base 2?
In the decimal system (Base 10), we use ten distinct digits: . When we compute , we reach the maximum capacity of a single digit and perform a "Carry" operation to the tens column, resulting in .
In the binary system (Base 2), the principle is exactly the same, but we only have two symbols available: . Consequently, carry operations occur much more frequently. Even in the simple addition of , we have no specific symbol for the value 'two', so the result is written as (zero in the current column and a carry of to the next column on the left).
Formal Definition of Positional Representation
Let be a positive integer. Its -bit binary representation is defined as a sequence of bits where each bit , satisfying the following equation:
1. Binary Addition
Given two binary numbers and , the addition operation is defined recursively for each column (starting from ) by calculating the sum bit and the next carry bit :Where (no initial carry), and the operators denote the logical XOR, AND, and OR operations respectively.
2. Binary Subtraction & Two's Complement
Direct subtraction is based on a "Borrow" mechanism from the next column. A borrow from column to column adds () to the current column.In modern computing systems, to optimize hardware and eliminate complex borrow logic, negative numbers are represented using Two's Complement.
Definition: For a positive integer represented in bits, its two's complement (representing ) is defined as:
Where is the bitwise inversion of (applying a NOT operation to each bit). Thus, the subtraction is transformed entirely into modular addition:
Edge Cases
1. Overflow: Two distinct types must be distinguished:
- Unsigned Overflow: — the final carry bit exceeds the -bit width. Example (4 bits): ; in 4 bits this yields — an error.
- Signed Overflow (Two's Complement): Both operands have the same sign (same MSB), but the sign of the result differs. Quick detection rule: signed overflow (carry into MSB ≠ carry out of MSB). Example: — and yield in 4-bit signed representation: signed overflow.
2. Cascading Borrow: Subtracting a number from a value containing a long sequence of zeros (e.g., ). In this scenario, the borrow operation propagates through all intermediate columns until it hits the first available bit.
UI Visualization Ideas
- Interactive Bit-Matrix: A visual interface where the student can enter two binary numbers and step through the execution. With each click, the active column is highlighted, and a small animated arrow in a prominent color (e.g., yellow or cyan) carries the bit over to the next column on the left.
- Two's Complement Inversion Animation: A component showing a positive binary number passing through an inversion gate that flips all zeros to ones and vice versa, followed by a increment dropping into the least significant bit with a pulse effect.
Interactive Visualization
Loading visualization
Worked Examples
Practice
Exercise 1 of 5
1
What is the result of adding the binary numbers ?
★Quiz
Test your knowledge from this lesson
A mix of questions from the lesson — each attempt varies slightly
Answer at least 4 out of 5 questions correctly to pass (70%)