Data Representation
Data Representation
Computers store and process data as numbers, specifically in binary. Since modern computer architecture relies on transistors which are either On (1) or Off (0)
A light switch has two states, on and off. Computers work the same way with 1s and 0s.
The Representation of Integers in Binary
Binary
Binary is a base-2 number system, using only two digits: 0 and 1. Each digit in a binary number is called a bit (short for binary digit).
Place Value in Binary
- Each bit position represents a power of 2, starting from $2^0$ at the rightmost bit and increasing to the left.
- Example: The binary number $1011_2$ can be broken down as follows:
| Bit | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 |
|---|---|---|---|---|---|---|---|---|
| Place Value | $$2^7$$ | $$2^6$$ | $$2^5$$ | $$2^4$$ | $$2^3$$ | $$2^2$$ | $$2^1$$ | $$2^0$$ |
| Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Decimal Value | 0 | 0 | 0 | 0 | 8 | 0 | 2 | 1 |
Calculation: $1 \times 8 + 0 \times 4 + 1 \times 2 + 1 \times 1 = 11_{10}$
Binary is the most compact representation for computers, as it directly aligns with their hardware, which uses transistors operating in two states.
Converting Binary to Decimal
- Identify the Place Values: Each bit represents a power of 2.
- Multiply Each Bit by Its Place Value: For each 1 in the binary number, multiply it by the corresponding power of 2.
- Add the Products: Sum the results to get the decimal value.
Convert $1011_2$ to decimal.
Solution
- Place Values: $2^3, 2^2, 2^1, 2^0$
- Multiplication: $1 \times 8 + 0 \times 4 + 1 \times 2 + 1 \times 1$
- Sum: $8 + 0 + 2 + 1 = 11_{10}$
- When converting binary to decimal, it can be helpful to read the binary number from right to left, aligning each bit with its corresponding power of 2.
- Draw it out in a table if necessary
| Place Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Binary | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
| Calculation | (128 x 1) | + 0 | + (32 x 1) | + (16 x 1) | + 0 | + 0 | + (2 x 1) | + (1 x 1) |
Converting Decimal to Binary
- Divide the Decimal Number by 2: Record the remainder (0 or 1).
- Repeat: Continue dividing the quotient by 2 until it reaches 0.
- Write the Remainders in Reverse Order: This forms the binary number.
Convert $13_{10}$ to binary.
Solution
- $13 \div 2 = 6$ R1
- $6 \div 2 = 3$ R0
- $3 \div 2 = 1$ R1
- $1 \div 2 = 0$ R1
Binary: $1101_2$
Method 2: Subtraction Method
- Start from largest power of 2 that fits
- Subtract it and write a 1
- Write 0s for any place values that don’t fit
Convert denary value 13 to binary
Solution
- 8 fits → 13 - 8 = 5 (so 1 in the 8 column)
- 4 fits → 5 - 4 = 1 (so 1 in the 4 column)
- 2 doesn’t fit (so 0 in the 2 column)
- 1 fits → 1 - 1 = 0 (so 1 in the 1 column)
- Binary = 1101, or 00001101 (8-bit)
- Binary numbers are often represented in 8 bits (a byte).
- If the binary number has fewer than 8 bits, pad with zeros on the left.
The Representation of Integers in Hexadecimal
Hexadecimal
Hexadecimal is a base-16 number system, using 16 digits: 0–9 and A–F (where A = 10, B = 11, ..., F = 15).
Why Use Hexadecimal?
- Human-Readable: More compact than binary, making it easier to read and write.
- Efficient for Computers: Each hexadecimal digit corresponds to 4 bits (a nibble), simplifying conversion between binary and hexadecimal.
Converting Hexadecimal to Decimal
- Identify the Place Values: Each digit represents a power of 16.
- Convert Letters to Decimal: A = 10, B = 11, ..., F = 15.
- Multiply Each Digit by Its Place Value.
- Add the Products.
Convert $2A_{16}$ to decimal.
Solution
- Place Values: $16^1, 16^0$
- Multiplication: $2 \times 16 + 10 \times 1$
- Sum: $32 + 10 = 42_{10}$
It can be helpful to read hexadecimal numbers from right to left, aligning each digit with its corresponding power of 16.
| Denary (Base 10) | Binary (Base 2) | Hexadecimal (Base 16) |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
Converting Decimal to Hexadecimal
- Divide the Decimal Number by 16: Record the remainder.
- Convert Remainders to Hexadecimal Digits: Remainders greater than 9 are converted to A–F.
- Write the Remainders in Reverse Order.
Convert $26_{10}$ to hexadecimal.
Solution
- $26 \div 16 = 1$ R10 (A)
- $1 \div 16 = 0$ R1
Hexadecimal: $1A_{16}$.
Conversion Between Binary and Hexadecimal
Binary to Hexadecimal
- Group Binary Digits into Nibbles: Start from the right.
- Convert Each Nibble to Hexadecimal.
Convert $11010110_2$ to hexadecimal.
Solution
- Group: $1101$ $0110$
- Convert: $1101 = D$, $0110 = 6$
- Hexadecimal: $D6_{16}$
If the binary number doesn't divide evenly into nibbles, add leading zeros to the leftmost group.
Hexadecimal to Binary
- Convert Each Hexadecimal Digit to a 4-Bit Binary Equivalent.
- Combine the Binary Groups.
Convert $D6_{16}$ to binary.
Solution
- Convert: $D = 1101$, $6 = 0110$
- Binary: $11010110_2$
Each hexadecimal digit directly maps to a 4-bit binary group, making conversion between these systems straightforward.
Representing negative numbers in binary
Two's Complement
Two's complement is a method for representing signed (positive, negative, and zero) binary numbers in computers. It's the most common way to represent integers in digital systems, enabling the use of the same hardware for both addition and subtraction operations.
- 4 bits can represent -8 to +7
- 8 bits can represent -128 to +127
How to find Two's Complement
To get the negative version of a number in binary:
- Write the number in binary (e.g. 00001101 for 13)
- Flip all the bits (change 0 → 1, and 1 → 0) so 11110010
- Add 1 (11110010 + 00000001 = 11110011)
- Final answer: 11110011
Why Binary and Hexadecimal Matter
- Binary: The foundation of all digital systems, aligning with the on/off states of transistors.
- Hexadecimal: A more human-readable representation, used in programming, memory addresses, and color codes.
- How does the use of binary and hexadecimal systems in computing shape our understanding of numerical representation and data processing?
- What implications does this have for the broader field of digital technology?