Comp Computer Science

Computer ScienceUnit 210 min read

Number Systems & Conversion: Binary, Octal, Hex, Decimal & Operations

Unit 2 of Computer Science teaches how computers represent numbers in binary, octal, hexadecimal, and decimal systems, how to convert between them, and perform arithmetic operations in binary—essential skills for understanding how computers process data at the hardware level.

TAKEAWAYS:

  • Computers use binary (base-2) numbers internally, but humans use decimal (base-10); conversions between them are fundamental.
  • Octal (base-8) and hexadecimal (base-16) are shortcuts for writing long binary numbers compactly.
  • Conversion rules (division-remainder, grouping, and positional value) simplify switching between bases.
  • Binary arithmetic (addition, subtraction, multiplication) follows simple rules but requires careful handling of carries.
  • NEB exam tests conversions, binary operations, and real-world applications (e.g., IP addresses in binary).

What is a Number System?

A number system is a way to represent numbers using symbols and rules. Different systems use different bases (the number of digits/symbols used). For example:

  • Decimal (base-10): Uses digits 0–9 (e.g., 256).
  • Binary (base-2): Uses digits 0–1 (e.g., 1010).
  • Octal (base-8): Uses digits 0–7 (e.g., 372).
  • Hexadecimal (base-16): Uses digits 0–9 and letters A–F (e.g., 1A3).

Why do we need different systems? Computers use binary because it’s simple (only two states: on/off, 1/0). Humans use decimal because it’s natural (10 fingers). Octal and hexadecimal are used to simplify binary for humans.


Binary Number System (Base-2)

Binary is the language of computers. Each digit is called a bit (binary digit). The value of each bit depends on its position (right to left, starting at 0):

Bit 7 (128)1Bit 6 (64)0Bit 5 (32)1Bit 4 (16)1Bit 3 (8)0Bit 2 (4)1Bit 1 (2)0Bit 0 (1)1
Binary positional values for the example `10110101` (173 in decimal)


graph LR
    B7["2^7 (128)"] --> B6["2^6 (64)"]
    B6 --> B5["2^5 (32)"]
    B5 --> B4["2^4 (16)"]
    B4 --> B3["2^3 (8)"]
    B3 --> B2["2^2 (4)"]
    B2 --> B1["2^1 (2)"]
    B1 --> B0["2^0 (1)"]

Example: Convert 1011 (binary) to decimal.

  • Break it down:
  • Total: (decimal).

Trace: Convert 11010 to decimal.

  1. Write powers of 2: .
  2. Multiply each bit by its power:
  3. Answer: .

Decimal to Binary Conversion

Use the division-remainder method:

  1. Divide the decimal number by 2.
  2. Write the remainder (0 or 1).
  3. Repeat until the quotient is 0.
  4. Read remainders from bottom to top.

Example: Convert 23 (decimal) to binary.

23 ÷ 2 = 11 (remainder 1)
11 ÷ 2 = 5  (remainder 1)
5  ÷ 2 = 2  (remainder 1)
2  ÷ 2 = 1  (remainder 0)
1  ÷ 2 = 0  (remainder 1)

Read remainders upward: 10111.

Trace: Convert 45 to binary.

45 ÷ 2 = 22 (1)
22 ÷ 2 = 11 (0)
11 ÷ 2 = 5  (1)
5  ÷ 2 = 2  (1)
2  ÷ 2 = 1  (0)
1  ÷ 2 = 0  (1)

Answer: 101101.


Octal (Base-8) Number System

Octal uses digits 0–7. It’s useful because 3 binary digits (bits) = 1 octal digit. This makes binary numbers shorter and easier to read.

Example: Convert 372 (octal) to decimal.

  • Break it down:
  • Total: (decimal).

Trace: Convert 506 (octal) to decimal.

  1. Answer: .

Hexadecimal (Base-16) Number System

Hexadecimal uses digits 0–9 and letters A–F (where A=10, B=11, ..., F=15). It’s even more compact than octal: 4 binary digits = 1 hex digit.

Example: Convert 1A3 (hex) to decimal.

  • Break it down:
  • Total: (decimal).

Trace: Convert FF (hex) to decimal.

  1. Answer: .

Conversion Between Binary, Octal, and Hexadecimal

Binary to Octal

Group binary digits into sets of 3 (from right to left). If needed, pad with leading zeros. Convert each group to octal.

036911Binary3 bitsOctal1 bitsBinary3 bitsOctal1 bitsBinary3 bitsOctal1 bitsBinary3 bitsOctal1 bits
Binary-to-octal conversion for `11010110` → `1530` (octal)

Example: Convert 11010110 (binary) to octal.

  1. Group: 001 101 011 0 (pad with one zero).
  2. Convert each group: 001 = 1 101 = 5 011 = 3 000 = 0
  3. Answer: 1530 (octal).

Trace: Convert 1011001 to octal.

  1. Group: 001 011 001 (pad with two zeros).
  2. Convert: 001 = 1 011 = 3 001 = 1 Answer: 131 (octal).

Binary to Hexadecimal

Group binary digits into sets of 4 (from right to left). Pad with leading zeros if needed. Convert each group to hex.

0481215Binary4 bitsHex1 bitsBinary4 bitsHex1 bits
Binary-to-hex conversion for `11010110` → `D6` (hex)

Example: Convert 11010110 (binary) to hex.

  1. Group: 1101 0110.
  2. Convert each group: 1101 = D 0110 = 6
  3. Answer: D6 (hex).

Trace: Convert 101100101 to hex.

  1. Group: 0001 0110 0101 (pad with three zeros).
  2. Convert: 0001 = 1 0110 = 6 0101 = 5 Answer: 165 (hex).

Binary Arithmetic

sequenceDiagram
    participant A as Binary Addition
    participant B as 1011
    participant C as 1101
    participant D as Result
    A->>B: Addend 1
    A->>C: Addend 2
    loop Carry
        A->>D: 1 (carry)
    end
    A->>D: 11000
    note right of D: 1011 + 1101 = 11000 (27 in decimal)
Step-by-step binary addition with carry propagation

Binary Addition

Add binary digits like decimal, but remember:

  • (write 0, carry 1)
  • (write 1, carry 1)

Example: Add 1011 + 1101.

  1011
+ 1101
-------
 11000

Trace: Add 1101 + 0110.

  1101
+ 0110
-------
 10011

Binary Subtraction

Subtract binary digits, borrowing as needed:

  • (borrow 1 from the next left digit)

Example: Subtract 1011 - 0110.

  1011
- 0110
-------
  0101

Trace: Subtract 1101 - 0011.

  1101
- 0011
-------
  1010

Binary Multiplication

Multiply like decimal, but use binary rules:

Example: Multiply 101 × 11.

    101
   ×  11
   -----
    101   (101 × 1)
   101    (101 × 1, shifted left)
   -----
  10011

Trace: Multiply 110 × 101.

    110
   × 101
   -----
    110   (110 × 1)
   000    (110 × 0, shifted left)
  110     (110 × 1, shifted left twice)
  -----
 100110

Comparison Table: Number Systems

Feature Decimal (Base-10) Binary (Base-2) Octal (Base-8) Hexadecimal (Base-16)
Digits Used 0–9 0–1 0–7 0–9, A–F
Base 10 2 8 16
Used By Humans Computers Legacy computing Modern computing
Example 256 100000000 400 100
Advantage Easy for humans Simple for computers Compact for binary Very compact for binary
Disadvantage Slow for computers Long for humans Limited use Requires learning A–F

Applications of Number Systems

  1. Binary in Computers:

    • Computers store data as binary (e.g., 10101010 for text or images).
    • Example: IP addresses like 192.168.1.1 are binary at the hardware level.
  2. Hexadecimal in Programming:

    • Used to represent colors (e.g., #FF0000 = red).
    • Memory addresses in programming (e.g., 0x0040).
  3. Octal in Legacy Systems:

    • Older computers (e.g., PDP-8) used octal for simplicity.

Common Mistakes to Avoid

  1. Forgetting to pad zeros when converting binary to octal/hex.

    • Wrong: 10110 → 101 10 (missing a zero).
    • Correct: 010 110 → 26 (octal).
  2. Misaligning bits when grouping for octal/hex conversion.

    • Always start grouping from the right.
  3. Incorrect carry handling in binary addition/subtraction.

    • Practice with small numbers first.

NEB Board-Style Questions

Short Answer (2 marks)

  1. Convert 101101 (binary) to decimal. Answer: .

  2. Write 47 (decimal) in binary. Answer: 101111.

  3. What is the hexadecimal equivalent of 11011010 (binary)? Answer: DA.

Long Answer (5 marks)

  1. Convert 3A7 (hex) to binary and then to octal.

    • Step 1: Hex to binary: 3 = 0011, A = 1010, 7 = 0111 → 001110100111.
    • Step 2: Binary to octal: Group: 001 110 100 111 → 1647 (octal).
  2. Perform the following binary operations: a) 1101 + 1011 Answer: 11000. b) 10110 - 1101 Answer: 01001. c) 1101 × 101 Answer: 1000111.


Exam Tip

  1. Memorize conversion shortcuts:

    • Binary to octal: Group 3 bits.
    • Binary to hex: Group 4 bits.
  2. Practice binary arithmetic:

    • NEB often tests addition/subtraction. Write steps clearly.
  3. Watch units:

    • (hex), (octal). Know powers of 2, 8, and 16.
  4. Real-world connections:

    • IP addresses (e.g., 192.168.1.1) are binary at the core.
    • Colors in web design use hex (e.g., #00FF00 = green).
  5. Time management:

    • If stuck on a conversion, move to the next question and return later.

Summary Checklist

Before the exam, ensure you can: ✅ Convert decimal ↔ binary using division-remainder. ✅ Convert binary ↔ octal/hex using grouping. ✅ Perform binary addition, subtraction, and multiplication. ✅ Recognize applications (IP addresses, colors, memory). ✅ Avoid common mistakes (padding, carry handling).

Based on the NEB +2 Management syllabus for Computer Science (Comp), unit 2.

Discussion

Loading…