Computer Science – Number Systems and Data Representation
Computers store integers as patterns of bits. This tutorial explains how binary works, how to perform arithmetic, and how signed magnitude and two’s complement represent negative values.
Learning objectives
- Convert positive decimal integers to binary and back.
- Add and subtract binary integers.
- Explain signed magnitude and two’s complement.
- Represent signed integers using a specified number of bits.
- Determine the range of integers supported by two’s complement.
1. Understanding binary numbers
A bit is a binary digit: 0 or 1. Decimal (base 10) uses powers of 10; binary (base 2) uses powers of 2. The place values, from right to left, are 1, 2, 4, 8, 16, 32 and so on.
| Binary place value | 8 | 4 | 2 | 1 |
|---|---|---|---|---|
| Binary 0110 | 0 | 1 | 1 | 0 |
Convert binary to decimal
Multiply each binary digit by its place value and add the results.
110₂ = (1 × 4) + (1 × 2) + (0 × 1) = 6₁₀. Similarly, 11₂ = (1 × 2) + (1 × 1) = 3₁₀.
The subscript ₂ means binary and ₁₀ means decimal.
2. Converting decimal to binary
Repeatedly divide the positive decimal number by 2, recording the quotient and remainder. Stop when the quotient becomes 0. Read the remainders from bottom to top.
Worked example: Convert 13₁₀ to binary
| Division | Quotient | Remainder |
|---|---|---|
| 13 ÷ 2 | 6 | 1 |
| 6 ÷ 2 | 3 | 0 |
| 3 ÷ 2 | 1 | 1 |
| 1 ÷ 2 | 0 | 1 |
Reading the remainders upwards gives 13₁₀ = 1101₂. Check: 8 + 4 + 0 + 1 = 13.
Try it: Convert 6, 9 and 18 to binary before reading the answers below.
3. Binary addition
| Operation | Result |
|---|---|
| 0 + 0 | 0 |
| 0 + 1 | 1 |
| 1 + 0 | 1 |
| 1 + 1 | 10 (write 0, carry 1) |
Worked example: Add 6 and 3
6₁₀ = 0110₂ and 3₁₀ = 0011₂.
0110
+ 0011
------
1001
Therefore, 0110₂ + 0011₂ = 1001₂. Check: 6 + 3 = 9, and 1001₂ = 8 + 1 = 9. Add from the right, carrying when a column totals 2 or 3.
4. Binary subtraction
Subtract from right to left. When you encounter 0 − 1, borrow from the next available 1 to the left. A borrowed 1 represents binary 10 (decimal 2) in the current column.
Worked example: Subtract 3 from 6
0110
- 0011
------
0011
In the rightmost column, 0 − 1 requires borrowing. The next column then also requires borrowing from the column to its left. The answer is 0011₂ = 3₁₀, as expected.
5. Signed magnitude representation
For signed magnitude, the most significant bit (MSB) is the sign: 0 for positive, 1 for negative. The remaining bits give the magnitude (absolute value). The number of bits must be specified.
| Decimal | 8-bit signed magnitude |
|---|---|
| +6 | 00000110 |
| −6 | 10000110 |
| +3 | 00000011 |
| −3 | 10000011 |
An 8-bit signed-magnitude system represents values from −127 to +127. It has two encodings for zero: positive zero (00000000) and negative zero (10000000).
6. Two’s complement representation
Two’s complement is widely used for signed integers. To represent a negative number with a fixed number of bits:
- Write the positive integer in binary using the required bit width.
- Invert every bit (0 becomes 1; 1 becomes 0).
- Add 1.
Worked example: Represent −6 using 8 bits
Step 1: +6 = 00000110
Step 2: Invert all bits → 11111001
Step 3: Add 1.
11111001 + 1 ---------- 11111010
Therefore, −6 = 11111010 in 8-bit two’s complement.
Shortcut: Copy and invert
Starting at the right, copy digits up to and including the first 1; then invert every digit further left. Applied to 00000110, this produces 11111010.
Comparing signed magnitude and two’s complement
| Decimal | Signed magnitude (8 bits) | Two’s complement (8 bits) |
|---|---|---|
| +6 | 00000110 | 00000110 |
| −6 | 10000110 | 11111010 |
| +3 | 00000011 | 00000011 |
| −3 | 10000011 | 11111101 |
Important: The same bit pattern can mean different values depending on which representation is used. The MSB of a two’s complement number has a negative place value, unlike the separate sign bit in signed magnitude.
Optional concept explanations: The next two notes go beyond the basic conversion and arithmetic procedures. They are provided for students who want to understand why two’s complement works. You may skip them on a first reading and return to them after practising the examples.
Concept note: Why the leftmost bit is negative in two’s complement
Reconciliation note: In a fixed-width two’s complement number, the most significant bit (MSB) carries a negative place value. It is not a separate sign bit as it is in signed magnitude. For three bits, the weights from left to right are −4, +2 and +1, instead of +4, +2 and +1.
There are 2³ = 8 different three-bit patterns. Using two’s complement, these represent all integers from −4 through +3:
| Decimal value | 3-bit two’s complement | Using weights −4, +2, +1 |
|---|---|---|
| 0 | 000 | 0 |
| +1 | 001 | 1 |
| +2 | 010 | 2 |
| +3 | 011 | 2 + 1 |
| −1 | 111 | −4 + 2 + 1 |
| −2 | 110 | −4 + 2 |
| −3 | 101 | −4 + 1 |
| −4 | 100 | −4 |
Check the negatives: 111 = −4 + 2 + 1 = −1; 110 = −4 + 2 = −2; 101 = −4 + 1 = −3; and 100 = −4.
Why this makes sense: A three-bit system has eight patterns but cannot represent every integer. By giving the MSB a weight of −4, two’s complement uses those patterns to represent four non-negative values (0 to +3) and four negative values (−1 to −4), with only one representation of zero. This is a mathematical interpretation of the two’s complement system, not a historical claim about its invention.
One subtlety: In binary pattern order, 011 is followed by 100. As signed three-bit values, those mean +3 and −4. Adding 1 to +3 therefore overflows the signed range; it does not mean +3 + 1 equals −4 in ordinary mathematics.
Concept note: Why do we add 1 in two’s complement?
First, an important distinction: It is one’s complement (and signed magnitude), not two’s complement, that has both positive zero and negative zero. In one’s complement, changing a positive value to its negative requires only flipping every bit. For example, in 3 bits, 000 is +0 and 111 is −0.
Two’s complement uses invert the bits, then add 1. That extra 1 changes the negative-number patterns so 111 becomes −1 instead of −0, while 000 is the only zero. The freed-up bit pattern 100 represents −4, giving the 3-bit two’s complement range −4 to +3.
Compare every possible 3-bit pattern:
| 3-bit pattern | One’s complement value | Two’s complement value |
|---|---|---|
000 | +0 | 0 |
001 | +1 | +1 |
010 | +2 | +2 |
011 | +3 | +3 |
100 | −3 | −4 |
101 | −2 | −3 |
110 | −1 | −2 |
111 | −0 | −1 |
Worked example: Find −2 using 3-bit two’s complement. Start with +2 = 010. Flip its bits to obtain 101 (this is −2 in one’s complement). Add 1: 101 + 001 = 110. In two’s complement, 110 is −2 because (1 × −4) + (1 × 2) + (0 × 1) = −2.
What happens to negative zero? Invert 000 to get 111, then add 1: 111 + 001 = 1000. With only 3 bits, discard the carry beyond the third bit, leaving 000. So negative zero becomes the same bit pattern as zero: no separate −0.
Why the arithmetic works: step by step (optional)
Step 1 — There are eight patterns. With 3 bits, there are 2³ = 8 possible patterns, from 000 to 111. The largest unsigned 3-bit value is 7 (111).
Step 2 — Flipping bits gives the complement to 7, not to 8. Let us start with +2, written as 010. When we flip every bit, we obtain 101, which is 5 if read as an unsigned number. Notice that 2 + 5 = 7 (binary 111), not 8. This is why simply inverting the bits gives one’s complement.
Step 3 — Adding 1 completes the full eight. Add 1 to 101 to get 110 (6 as an unsigned value). Now 2 + 6 = 8, which is 1000 in binary. In a fixed-width 3-bit operation, the leftmost carry is discarded, leaving 000. Thus 110 acts as −2 when added to +2 using 3-bit arithmetic.
010 (+2) + 110 (−2 in 3-bit two’s complement) ----- 1000 (discard the carry beyond 3 bits) 000 (zero)
Step 4 — That lets subtraction use addition. To calculate 3 − 2, write +3 as 011 and −2 as 110. Add them as ordinary binary numbers: 011 + 110 = 1001. Keep the lowest 3 bits, 001, which equals +1. Therefore, 3 + (−2) = 1.
General rule: With n bits, flipping the bits of a number x produces (2n − 1) − x. Adding 1 produces 2n − x. Consequently, x + (2n − x) = 2n, whose lowest n bits are all zero. This is the mathematical reason for the “+1”. It gives an additive inverse using fixed-width binary arithmetic and also eliminates the separate negative-zero representation found in one’s complement.
Important limitation: This works for results representable within the chosen signed bit width. A calculation outside the range (−4 to +3 for three bits) causes signed overflow, so discarding a carry must not be mistaken for a valid result in every case.
7. Subtraction using two’s complement
Subtraction can be rewritten as addition: 6 − 3 = 6 + (−3).
Represent +6 as 00000110 and −3 as 11111101 in 8-bit two’s complement.
00000110 (+6) + 11111101 (−3) ---------- 1 00000011
Discard the carry beyond the eighth bit. The 8-bit result is 00000011 = +3. In signed arithmetic, also check whether the true result fits the selected bit width; discarding the carry alone does not guarantee that overflow has not occurred.
8. Finding the largest and smallest two’s complement integers
For an n-bit two’s complement system, the largest integer is 2n−1 − 1 and the smallest is −2n−1.
Worked example: A 4-bit system
Largest: 2³ − 1 = +7. Smallest: −2³ = −8.
| 4-bit two’s complement | Decimal |
|---|---|
| 0111 | +7 |
| 0000 | 0 |
| 1111 | −1 |
| 1000 | −8 |
The 4-bit two’s complement range is −8 to +7, whereas 4-bit signed magnitude represents −7 to +7.
9. Common mistakes to avoid
| Mistake | Correct approach |
|---|---|
| Reading division remainders from the top | Read them from the bottom to the top. |
| Forgetting to carry in binary addition | Carry 1 when a column totals binary 10 or 11. |
| Changing only the sign bit for two’s complement | Invert all bits and add 1. |
| Using an inconsistent number of bits | Keep a fixed, stated bit width for signed calculations. |
| Assuming every discarded carry means a valid signed result | Check for signed overflow as well. |
| Assuming the greatest 4-bit signed integer is 15 | It is +7 in 4-bit two’s complement. |
10. Practice exercises
- Convert 19₁₀ to binary.
- Convert 101101₂ to decimal.
- Add 1011₂ and 0110₂.
- Subtract 0101₂ from 1100₂ using ordinary binary subtraction.
- Represent −9 in 8-bit two’s complement.
- Calculate 7 − 6 using 8-bit two’s complement addition.
- State the lowest and highest integers that 6-bit two’s complement can represent.
Answers
| Question | Answer |
|---|---|
| 1 | 10011₂ |
| 2 | 45₁₀ |
| 3 | 10001₂ (17₁₀) |
| 4 | 0111₂ (7₁₀) |
| 5 | 11110111 |
| 6 | 00000001 (+1) |
| 7 | −32 to +31 |
11. Related lessons and further practice
- Binary Representation Live Class – October 14, 2026
- Data Representation Homework Exercise
- Decimal to Binary – Flowgorithm and C
Final takeaway
Binary representation is more than memorising strings of zeros and ones. Understanding place values, carrying, borrowing, signed magnitude and two’s complement helps explain how computers store and manipulate integer data. Verify answers by converting back to decimal whenever possible.
© 2026 Vedesh Kungebeharry. All rights reserved.