OCR A-Level Computer Science: Data Representation
This tutorial explores the fundamental concepts of data representation in computers, including binary, hexadecimal, character encoding, and bitwise operations.
1. Binary and Hexadecimal Number Systems
Computers operate on a binary system, using only two digits: 0 and 1. Each digit represents a bit (binary digit).
a) Binary Representation:
- Each position in a binary number represents a power of 2.
- Example:
- 10112 = (1 x 23) + (0 x 22) + (1 x 21) + (1 x 20) = 8 + 0 + 2 + 1 = 1110
b) Hexadecimal Representation:
- Hexadecimal uses 16 digits: 0-9 and A-F, where A=10, B=11, C=12, D=13, E=14, and F=15.
- It is often used for representing binary data in a more compact format.
- Each position represents a power of 16.
- Example:
- 2A516 = (2 x 162) + (10 x 161) + (5 x 160) = 512 + 160 + 5 = 67710
c) Conversion:
- Binary to Decimal: Sum the values of each bit based on its position (power of 2).
- Decimal to Binary: Repeatedly divide the decimal number by 2, noting the remainder (0 or 1) at each step. The remainders, read from bottom to top, form the binary representation.
- Binary to Hexadecimal: Group binary digits into sets of 4, starting from the rightmost digit. Convert each group into its hexadecimal equivalent.
- Hexadecimal to Binary: Convert each hexadecimal digit into its 4-bit binary representation.
2. Representing Integers: Sign-and-Magnitude and Two's Complement
a) Sign-and-Magnitude:
- The leftmost bit represents the sign (0 for positive, 1 for negative).
- The remaining bits represent the magnitude of the number.
- Example:
- 01012 = +510
- 11012 = -510
- Limitation: Two representations for zero (0000 and 1000).
b) Two's Complement:
- The most common method for representing signed integers.
- For a positive number, it is the same as its binary representation.
- For a negative number, it is calculated by:
- Invert all bits of the positive equivalent.
- Add 1 to the result.
- Example:
- +510 = 01012
- -510 = 10112 (invert 0101, add 1: 1010 + 1 = 1011)
- Advantages:
- Single representation for zero.
- Arithmetic operations can be performed directly without considering sign.
3. Character Encoding
Computers store text as numerical values representing characters.
a) ASCII (American Standard Code for Information Interchange):
- Uses 7 bits to represent 128 characters, including uppercase and lowercase letters, numbers, punctuation marks, and control characters.
- Example:
- 'A' = 6510 = 010000012
- 'a' = 9710 = 011000012
b) Unicode:
- Extends ASCII to represent characters from different languages and scripts.
- Uses 16 bits (UTF-16) or 32 bits (UTF-32).
- Example:
- 'é' = 23310 = 00000000 111010012 (UTF-16)
4. Bitwise Operations
These operations work on individual bits of data.
a) AND ( & ):
- Returns 1 if both corresponding bits are 1, 0 otherwise.
- Example: 1011 & 0101 = 0001
b) OR ( | ):
- Returns 1 if at least one corresponding bit is 1, 0 otherwise.
- Example: 1011 | 0101 = 1111
c) XOR ( ^ ):
- Returns 1 if exactly one corresponding bit is 1, 0 otherwise.
- Example: 1011 ^ 0101 = 1110
d) NOT ( ~ ):
- Flips the bits of a number (0 becomes 1, 1 becomes 0).
- Example: ~ 1011 = 0100
Summary
Understanding data representation is crucial for working with computers. This tutorial covered binary, hexadecimal, character encoding, and bitwise operations, providing a foundation for understanding how data is stored, manipulated, and processed within a computer system.