File handling
Using binary is beneficial because:
- It is physically reliable: Distinguishing between 'on' and 'off' is easier than distinguishing between 10 different voltage levels (as in denary).
- It reduces noise errors: Electrical interference might change a precise voltage slightly, but it rarely flips a switch from fully ON to fully OFF.
Building on this, all data (text, images, sound) must be converted into binary patterns for the CPU to process.
A number system is defined by its base (or radix), which determines how many unique digits are used and how place values increase.
| System | Base | Digits Used | Place Values |
|---|---|---|---|
| Denary (Decimal) | 10 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 | Powers of 10 (1, 10, 100...) |
| Binary | 2 | 0, 1 | Powers of 2 (1, 2, 4, 8...) |
| Hexadecimal | 16 | 0-9, A-F | Powers of 16 (1, 16, 256...) |
Note on Hexadecimal:
- Digits A to F represent denary values 10 to 15.
- A=10, B=11, C=12, D=13, E=14, F=15.
To convert a positive denary integer to binary, use the powers of 2 method (also known as the subtraction method).
Algorithm:
- List powers of 2 (1, 2, 4, 8, 16, 32, 64, 128...) until you exceed the denary number.
- Find the largest power of 2 that fits into the denary number. Place a 1 in that position and subtract that value from the denary number.
- Move to the next lower power of 2. If it fits into the remaining value, place a 1 and subtract. If it does not fit, place a 0.
- Repeat until you reach 2⁰ (the ones column).
Example: Convert Denary 15 to Binary
- Powers of 2: 8, 4, 2, 1
- Does 8 fit in 15? Yes. 15 - 8 =
- Bit: 1
- Does 4 fit in 7? Yes. 7 - 4 =
- Bit: 1
- Does 2 fit in 3? Yes. 3 - 2 =
- Bit: 1
- Does 1 fit in 1? Yes. 1 - 1 =
- Bit: 1
- Result: 1111_2
Correct Understanding:
In computer systems, data is often stored in fixed-size registers (e.g., 8 bits). You must pad with leading zeros to fill the required width.
- Denary 5 in binary is 101.
- Denary 5 as an 8-bit integer is 00000101.
Always check if the question specifies a bit-width (e.g., '8-bit'). If it does, ensure your answer has exactly that many digits.
To convert a positive denary integer to hexadecimal, use the division by 16 method.
Algorithm:
- Divide the denary number by 16.
- Record the remainder. This is the least significant digit (rightmost).
- Take the quotient and divide it by 16 again.
- Repeat until the quotient is 0.
- The hexadecimal value is the sequence of remainders read from bottom to top (last remainder is the most significant digit).
Example: Convert Denary 301 to Hexadecimal
- 301 ÷ 16 = 18 with a remainder of 13 (D)
- 18 ÷ 16 = 1 with a remainder of 2
- 1 ÷ 16 = 0 with a remainder of 1
- Read remainders upwards: 1, 2, D
- Result: 12D_{16}
Hexadecimal is a shorthand for binary. Each single hexadecimal digit corresponds exactly to 4 binary bits (a nibble). This makes conversion very fast.
Algorithm:
- Break the hexadecimal number into individual digits.
- Convert each digit separately into its 4-bit binary equivalent.
- Concatenate the groups.
| Hex Digit | Binary Nibble |
|---|---|
| 0 | 0000 |
| 1 | 0001 |
| . | . |
| A (10) | 1010 |
| E (14) | 1110 |
| F (15) | 1111 |
Example: Convert Hex E3 to Binary
- Digit E (14 in denary) → 1110
- Digit 3 (3 in denary) → 0011
Result: 11100011_2
Why Examiners Accept This: Hexadecimal is used because it provides a more compact and human-readable representation of binary data. Since each hex digit represents 4 bits, long binary strings (like memory addresses or color codes) are significantly shorter in hex. For example, the 8-bit binary 11100011 is written as just two characters (E3) in hexadecimal. This reduces the chance of transcription errors by humans and makes debugging easier.
Look for phrases like: 'Hexadecimal is more compact than binary' or 'It is easier to read/write than long strings of 0s and 1s'. Avoid saying it is 'faster for the computer'—the CPU processes binary; hex is just a human convenience.
Rules:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1-1 + 1 = 10 (write 0, carry 1)
- 1 + 1 + 1 = 11 (write 1, carry 1)
Example: Add 00110011_2 and 01100001_2
```
Carry: 111111
00110011
- 01100001
10010100
```
- Rightmost column: 1+1=10 (write 0, carry 1)
- Next: 1+0+1=10 (write 0, carry 1)
- Next: 1+0+0=1 (write 1)
-...and so on.
Result: 10010100_2
Correct Understanding:
In an 8-bit system, the maximum positive value is 11111111_2 (255_{10}). If you add two numbers and the result requires 9 bits (i.e., there is a carry out of the MSB), an overflow has occurred.
- Why it occurs: The register size is fixed. The extra bit cannot be stored, so the result wraps around or becomes incorrect.
- Identification: If adding two positive numbers results in a negative-looking number (MSB becomes 1) or if there is a carry out of the MSB that is lost, overflow has happened.
Example:
128 + 129 = 257. In 8-bit binary:
01000000 + 10000001 = 11000001 (with a carry out of 1).
The stored result 11000001 is interpreted as -63 in two's complement, which is wrong. The carry bit was lost.
Why Examiners Accept This: You must state that the result is too large to be stored in the available bits. Specifically, mention that the result exceeds the maximum value representable by the bit-width (e.g., > 255 for 8-bit unsigned integers).
Acceptable phrases include:
- 'The result is greater than 255.'
- 'The result cannot be stored in 8 bits.'
- 'There is a carry out of the MSB that cannot be accommodated.'
Avoid vague answers like 'the computer crashed' or 'it is too big'. Be specific about the bit limit.
Left Shift (\ll):
- Moves bits to the left by n places.
- Fills rightmost positions with 0s.
- Effect: Multiplies the number by 2^n.
- Example: 101_2 (5_{10}) shifted left by 1 is 1010_2 (10_{10}). 5 \times 2^1 = 10.
Right Shift (\gg):
- Moves bits to the right by n places.
- Fills leftmost positions with 0s (for positive integers).
- Effect: Divides the number by 2^n (integer division, discarding remainders).
- Example: 1010_2 (10_{10}) shifted right by 1 is 0101_2 (5_{10}). 10 \div 2^1 = 5.
Note: Bits shifted out of the register are lost. This can cause data loss or overflow (in left shifts).
Original: 11100011
Shifted: 10001100
Result: 10001100_2
Range of 8-bit Two's Complement:
- The Most Significant Bit (MSB) is the sign bit.
- If MSB = 0, the number is positive.
- If MSB = 1, the number is negative.
- Range: -128 to +127.
Converting Denary Negative to Two's Complement Binary:
- Write the positive denary number in 8-bit binary.
- Invert all bits (change 0s to 1s and 1s to 0s). This is called the 'one's complement'.
- Add 1 to the result.
Example: Convert -5 to 8-bit two's complement
- Positive 5 in binary: 00000101
- Invert bits: 11111010
- Add 1: 11111010 + 1 = 11111011
Result: 11111011_2
To convert a two's complement binary number back to denary, you must determine if it is positive or negative first.
Algorithm:
- Check the MSB (leftmost bit).
- If MSB = 0: The number is positive. Convert normally using powers of 2.
- If MSB = 1: The number is negative. Proceed to step 2.
- Invert all bits (change 0s to 1s and 1s to 0s).
- Add 1 to the inverted result.
- Convert the resulting binary number to denary.
- Apply a negative sign to the final value.
Example: Convert 11110100_2 to Denary
- MSB is 1, so it is negative.
- Invert bits: 00001011
- Add 1: 00001011 + 1 = 00001100
- Convert 00001100_2 to denary:
- 8 + 4 = 12
- Apply negative sign: -12_{10}
Mistake: Assuming an 8-bit two's complement system can represent +128.
Correct Understanding:
In two's complement, the MSB has a negative weight (-2^7 = -128). The maximum positive value is when all other bits are 1: 01111111_2 = +127.
Therefore:
- You cannot represent +128 in 8-bit two's complement.
- Adding two positive numbers that result in an MSB of 1 indicates an overflow because the true sum exceeds +127.
- The minimum value is 10000000_2, which equals -128 (not -0).
Why Examiners Accept This: Always verify the sign bit. If a question claims 10000000_2 is positive 128, it is incorrect. In two's complement,10000000_2 is -128.
Look for students who explicitly state: 'The MSB is 1, so the number is negative' or 'The range of 8-bit two's complement is -128 to +127'. If a student converts -5 and gets 00000101, they failed to invert/add. The correct method requires showing the inversion step clearly.
- Positive 19 in binary: 00010011
- Invert bits: 11101100
- Add 1: 11101100 + 1 = 11101101
Result: 11101101_2
- MSB is 1, so it is negative.
- Invert bits: 00101001
- Add 1: 00101001 + 1 = 00101010
- Convert to denary:32 + 8 + 2 = 42
- Apply negative sign: -42_{10}
Notes built from years of past papers
- The exact keywords examiners look for
- The most common questions, year after year
- The common mistakes that cost marks
Free while we're in beta.