Convert between positive denary and positive binary

IGCSE Computer Science (0478) – Data Representation

1. Number Systems

1.1 Why computers use binary

  • Electronic circuits have two stable states: off (low voltage) and on (high voltage).
  • Representing data with 0 (off) and 1 (on) is reliable, cheap and fast – this is the basis of the binary system.
  • All other number systems (decimal, hexadecimal, octal) are simply convenient ways for humans to read and write the binary data.

1.2 Overview of the three bases used in the syllabus

  • Denary (Decimal) – base 10, digits 0‑9.
  • Binary – base 2, digits 0 and 1. Each position represents a power of 2.
  • Hexadecimal – base 16, digits 0‑9 and A‑F. Each position represents a power of 16 (or exactly four binary bits).

1.3 Quick conversion check – Hex ↔ Binary

Because one hex digit = four binary bits, conversion is immediate:

Hex digitBinary (4‑bit)
00000
10001
20010
30011
40100
50101
60110
70111
81000
91001
A1010
B1011
C1100
D1101
E1110
F1111

Example: 3F₁₆ → 0011 1111₂

2. Converting Positive Numbers (Denary ↔ Binary)

2.1 Binary → Decimal (positive)

  1. Write the binary number and label each bit with its power of 2, starting with 2⁰ on the far right.
  2. Multiply each bit by its corresponding power of 2.
  3. Sum all the products – the total is the decimal value.

Example: Convert 101101₂ to decimal.

Bit (bi)Power of 2 (2i)Product
1 (MSB)2⁵ = 3232
02⁴ = 160
12³ = 88
12² = 44
02¹ = 20
1 (LSB)2⁰ = 11

Sum = 32 + 0 + 8 + 4 + 0 + 1 = 45. Hence 101101₂ = 45₁₀.

2.2 Decimal → Binary (positive)

Both methods are accepted in the exam. Choose the one you find quickest.

2.2.1 Repeated Division by 2

  1. Divide the decimal number by 2.
  2. Write down the remainder (0 or 1).
  3. Use the quotient for the next division.
  4. Repeat until the quotient becomes 0.
  5. Read the remainders **bottom‑up** – this is the binary answer.

Example: Convert 156₁₀ to binary.

Division stepQuotientRemainder
156 ÷ 2780
78 ÷ 2390
39 ÷ 2191
19 ÷ 291
9 ÷ 241
4 ÷ 220
2 ÷ 210
1 ÷ 201

Remainders read from bottom to top give 10011100₂.

2.2.2 Largest‑Power (Subtraction) Method

  1. List powers of 2 up to the given number.
  2. Starting with the largest power ≤ the number, place a 1 and subtract that power.
  3. Move to the next lower power: place 1 if it fits into the remaining value, otherwise place 0.
  4. The sequence of 1s and 0s from highest to lowest power is the binary result.

Example: Convert 156₁₀ using the subtraction method.

Power (2ⁿ)ValuePlace (1/0)Remaining
2⁷1281156‑128 = 28
2⁶64028
2⁵32028
2⁴16128‑16 = 12
8112‑8 = 4
414‑4 = 0
200
2⁰100

Result: 10011100₂ (same as the division method).

3. Negative Numbers – Two’s‑Complement

3.1 What is two’s‑complement?

  • Standard method for representing signed integers in binary for the IGCSE.
  • For an n‑bit word the range is ‑2ⁿ⁻¹ … 2ⁿ⁻¹‑1.
  • The most‑significant bit (MSB) is the sign bit: 0 = positive, 1 = negative.

3.2 Converting a positive decimal to two’s‑complement (n bits)

  1. Convert the absolute value to binary (Section 2).
  2. Pad with leading zeros so the total length is exactly n bits.
  3. Invert every bit (0 → 1, 1 → 0).
  4. Add 1 to the inverted number (ignore any overflow beyond the n‑th bit).

Example: Represent ‑25 as an 8‑bit two’s‑complement number.

  1. 25 → 00011001₂ (already 8‑bit).
  2. Invert → 11100110₂.
  3. Add 1 → 11100111₂.

Thus ‑25₁₀ = 11100111₂ (8‑bit).

3.3 Converting two’s‑complement to decimal

  1. If the MSB is 0, treat the whole pattern as a normal positive binary number.
  2. If the MSB is 1:
    • Invert all bits.
    • Add 1.
    • Interpret the result as a positive decimal and prefix a minus sign.

Example: Convert 11010110₂ (8‑bit) to decimal.

  • MSB = 1 → negative.
  • Invert → 00101001₂.
  • Add 1 → 00101010₂ = 42₁₀.
  • Result → ‑42₁₀.

3.4 Overflow in two’s‑complement arithmetic

  • Overflow occurs when the true mathematical result lies outside the range ‑2ⁿ⁻¹ … 2ⁿ⁻¹‑1 for an n‑bit word.
  • In addition, overflow is detected when the carry **into** the sign bit differs from the carry **out of** the sign bit.

Example (addition overflow): 8‑bit numbers 01111111₂ (127) + 00000001₂ (1).

  • Binary sum = 10000000₂ (MSB = 1, interpreted as –128).
  • Since the correct result 128 cannot be represented in 8 bits, overflow has occurred.

3.5 Logical Shifts and overflow

Logical shifts move bits without preserving the sign. A shift can therefore change the sign bit and cause overflow.

Left‑shift overflow example: 01111111₂ (127) << 1 → 11111110₂ = –2 (sign bit flipped). The operation has overflowed the positive range.

Right‑shift on a negative two’s‑complement number: Logical right shift inserts a 0 on the left, so the sign is lost.

  • Example: 11100101₂ (‑27) >> 1 → 01110010₂ = 114₁₀, a completely different value.
  • For arithmetic right shift (preserving sign) the exam expects a logical shift unless explicitly stated otherwise.

4. Binary ↔ Hexadecimal Conversion

4.1 Binary → Hexadecimal

  1. Pad the binary number on the left with zeros so its length is a multiple of 4.
  2. Group the bits into 4‑bit “nibbles”.
  3. Convert each nibble to its hexadecimal digit (use the table in 1.3).

Example: 1011010011₂

  • Pad → 0010 1101 0011₂.
  • Groups: 0010 = 2, 1101 = D, 0011 = 3.
  • Result → 2D3₁₆.

4.2 Hexadecimal → Binary

  1. Replace each hex digit with its 4‑bit binary equivalent (see table in 1.3).

Example: 9A₁₆1001 1010₂.

5. Logical Shift Operations

  • Logical left shift (<<): all bits move left; a 0 is inserted at the right‑most position. Each shift multiplies the unsigned value by 2 (ignoring overflow).
  • Logical right shift (>>): all bits move right; a 0 is inserted at the left‑most position. Each shift divides the unsigned value by 2 (integer division).

Example (8‑bit unsigned word): 00110101₂ (53₁₀)

  • Left shift 1 place → 01101010₂ = 106₁₀.
  • Right shift 2 places → 00001101₂ = 13₁₀.

Overflow note: If a shift changes the sign bit of a two’s‑complement number, the result is no longer a correct representation of the original signed value – this is considered overflow.

6. Text Representation – ASCII & Unicode

6.1 ASCII

  • 7‑bit code (often stored in an 8‑bit byte) representing 128 characters: A‑Z, a‑z, digits, punctuation, and control codes.
  • Each character is identified by a decimal value (0‑127) which can be written in binary or hexadecimal.

Example: The character ‘G’

  • ASCII decimal = 71₁₀.
  • Binary = 01000111₂.
  • Hexadecimal = 47₁₆.

6.2 Unicode (UTF‑8) – exam focus

  • Extends ASCII to over a million characters, using 1‑4 bytes per character.
  • For the IGCSE you only need to know that the first 128 Unicode code points are identical to ASCII.

7. Storage Units & Simple File‑Size Calculations

7.1 Units of digital information

UnitSymbolBits
bitb1
byteB8
kilobyteKB8 × 1 024 = 8 192
megabyteMB8 × 1 024 × 1 024 = 8 388 608
gigabyteGB8 × 1 024³ = 8 589 934 592

7.2 Example – Image file size

A colour image of 640 × 480 pixels uses 24 bits per pixel (8 bits each for Red, Green, Blue).

  • Total bits = 640 × 480 × 24 = 7 372 800 bits.
  • Convert to kilobytes: 7 372 800 ÷ 8 ÷ 1 024 ≈ 900 KB.

7.3 Simple lossless compression – Run‑Length Encoding (RLE)

RLE stores a character followed by the number of consecutive repetitions.

Example: AAAAABBBCCDAA5A3B2C1D2A. Compare the length of the compressed string with the original to discuss efficiency.

8. Common Pitfalls & Exam Tips

  • Always start counting powers of 2 from 2⁰ on the right‑hand side.
  • When using the division method, write each remainder clearly; a missing digit invalidates the whole answer.
  • For two’s‑complement, pad to the required word length **before** inverting the bits.
  • After any binary conversion, perform a quick reverse‑check (binary → decimal or hex → binary) to catch mistakes.
  • Remember that leading zeros do not affect the value but are required for fixed‑length questions (e.g., 8‑bit or 16‑bit representations).
  • When a logical shift changes the sign bit, note that overflow has occurred – this is a common exam trap.
  • For hex‑binary work, use the 4‑bit table as a shortcut; it saves time and reduces errors.

Create an account or Login to take a Quiz

44 views
0 improvement suggestions

Log in to suggest improvements to this note.