Comp Computer Science

Computer ScienceUnit 29 min read

Number Systems, Conversion & Binary Operations

Unit 2 of Computer Science covers how computers represent numbers (binary, octal, hexadecimal), how to convert between them, and how to perform arithmetic in binary—essential for understanding how computers process data at their core.

TAKEAWAYS:

  • Computers use binary (base-2) numbers, but humans use decimal (base-10)—you must convert between them.
  • Octal (base-8) and hexadecimal (base-16) are shortcuts for writing long binary numbers.
  • Binary arithmetic (addition, subtraction, multiplication) follows simple rules, just like decimal math but with 0 and 1.
  • Signed numbers in binary use 1’s complement or 2’s complement to represent negative values.
  • ASCII and Unicode encode text as binary numbers so computers can store and process letters and symbols.
  • NEB exam questions test conversions, binary operations, and real-world applications (like memory storage).


---

### **1. Why Do We Need Different Number Systems?**
Computers only understand **binary** (0 and 1), but humans use **decimal** (0–9). To bridge this gap, we use:
- **Binary (Base-2):** Used by computers (fastest for circuits).
- **Octal (Base-8):** Used in older computer systems (shorter than binary).
- **Hexadecimal (Base-16):** Used in programming and memory addresses (easier to read than binary).

**Example:**
If a computer stores your age **25** in binary, how does it know it’s not the letter **"Y"** (which is also stored as binary)?

---

### **2. Binary Number System (Base-2)**
Binary uses only **two digits: 0 and 1**.
Each digit is called a **bit** (Binary digiT).
The rightmost bit is the **Least Significant Bit (LSB)**, and the leftmost is the **Most Significant Bit (MSB)**.

**How to read binary?**
Each position represents a power of 2:

128 64 32 16 8 4 2 1

**Example:**
Convert `1011` (binary) to decimal:

1×8 + 0×4 + 1×2 + 1×1 = 8 + 0 + 2 + 1 = 11 (decimal)


**NEB-style question:**
Convert `11010110` (binary) to decimal.

---

### **3. Decimal to Binary Conversion**
**Method 1: Division by 2 (Remainder Method)**
Divide the decimal number by 2 and note the remainder. Repeat until the quotient is 0. Read remainders **from bottom to top**.

**Example:**
Convert `25` (decimal) to binary:

25 ÷ 2 = 12 (remainder 1) 12 ÷ 2 = 6 (remainder 0) 6 ÷ 2 = 3 (remainder 0) 3 ÷ 2 = 1 (remainder 1) 1 ÷ 2 = 0 (remainder 1) Reading remainders from bottom: 11001

```mermaid
flowchart TD
    A[25 ÷ 2] -->|Remainder 1| B[12 ÷ 2]
    B -->|Remainder 0| C[6 ÷ 2]
    C -->|Remainder 0| D[3 ÷ 2]
    D -->|Remainder 1| E[1 ÷ 2]
    E -->|Remainder 1| F[0]
    F -->|Stop| G[Binary: 11001]

Method 2: Subtraction of Powers of 2 Find the largest power of 2 ≤ the number and subtract. Repeat.

Example: Convert 53 to binary:

53 – 32 = 21 → 1 (for 32)
21 – 16 = 5  → 1 (for 16)
5  – 4  = 1  → 1 (for 4)
1  – 1  = 0  → 1 (for 1)
Binary: **110101**

4. Octal (Base-8) Number System

Octal uses digits 0–7. Each octal digit represents 3 binary digits (bits). Why? Because 8 = 2³, so 3 binary digits can be grouped into 1 octal digit.

Example: Convert 153 (octal) to binary:

1 → 001
5 → 101
3 → 011
Binary: **001 101 011** (or `1101011` without leading zeros)

NEB-style question: Convert 247 (octal) to binary.


5. Hexadecimal (Base-16) Number System

Hex uses digits 0–9 and A–F (where A=10, B=11, ..., F=15). Each hex digit represents 4 binary digits (bits). Why? Because 16 = 2⁴, so 4 binary digits = 1 hex digit.

Example: Convert 3A7 (hex) to binary:

3 → 0011
A → 1010
7 → 0111
Binary: **0011 1010 0111** (or `1110100111` without leading zeros)

NEB-style question: Convert 1B3 (hex) to binary and decimal.


6. Binary to Octal & Hexadecimal (Grouping Method)

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.

Example: Convert 10110110 to octal:

1 011 011 0 → Pad to 8 bits: 0010110110
001 → 1
011 → 3
011 → 3
0   → 0
Octal: **1330**
flowchart TD
    A["10110110"] --> B["Pad to 8 bits: 0010110110"]
    B --> C["Group into 3: 001 011 011 0"]
    C --> D["Convert: 1 3 3 0"]
    D --> E["Octal: 1330"]

Binary to Hexadecimal:

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

Example: Convert 11010110 to hex:

1101 0110 → Pad to 8 bits: 00011010110
0001 → 1
1010 → A
0110 → 6
Hex: **1A6**

NEB-style question: Convert 110010110101 to octal and hexadecimal.


7. Binary Arithmetic

Binary Addition

Add bits like decimal, but:

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 1 = 0 (carry 1)
  • 1 + 1 + 1 (carry) = 1 (carry 1)

Example:

  1011
+ 1101
-------
 11000

Binary Subtraction (2’s Complement Method)

  1. Find the 2’s complement of the subtrahend (number to subtract).
    • Invert bits (1’s complement).
    • Add 1 to the least significant bit (LSB).
  2. Add the minuend (number from which we subtract) to the 2’s complement of the subtrahend.
  3. Discard the overflow bit.

Example: Subtract 0101 (5) from 1010 (10):

2’s complement of 0101:
Invert: 1010
Add 1: 1011
Now add:
  1010 (10)
+ 1011 (2’s complement of 5)
-------
10101 → Discard overflow: **0101 (5)**
flowchart TD
    A["Subtract 5 from 10"] --> B["2's complement of 5: 1011"]
    B --> C["Add 10 + 1011"]
    C --> D["10101 → Discard overflow: 0101"]
    D --> E["Result: 5"]

NEB-style question: Subtract 0110 from 1001 using 2’s complement.


8. Signed Binary Numbers (1’s Complement & 2’s Complement)

Computers represent negative numbers using:

  1. 1’s Complement: Invert all bits of the positive number.
    • Example: +5 = 0101, -5 = 1010
  2. 2’s Complement: Invert bits and add 1.
    • Example: +5 = 0101, -5 = 1011

Why 2’s complement?

  • Easier for subtraction and addition.
  • No need for a separate sign bit (the leftmost bit indicates sign).

Example: Represent -13 in 8-bit 2’s complement:

Positive 13: 00001101
Invert:     11110010
Add 1:      11110011

NEB-style question: Find the 8-bit 2’s complement representation of -27.


9. Binary Multiplication

Multiply like decimal, but:

  • 0 × anything = 0
  • 1 × anything = the number itself

Example: Multiply 1011 (11) by 101 (5):

      1011
    ×  101
    -------
      1011   (1011 × 1)
     0000    (1011 × 0, shifted left)
    1011     (1011 × 1, shifted left twice)
    -------
   110111    (55 in decimal)

10. Binary Division

Divide like decimal, but:

  • Only 0 or 1 can be subtracted.
  • If the divisor is larger than the dividend, write 0.

Example: Divide 110110 (54) by 1010 (10):

1010 ) 110110
     1010 (1)
     ----
       0111
       0000 (0)
       ----
        1110
        1010 (1)
        ----
         0000

Quotient: 101 (5), Remainder: 0


11. ASCII and Unicode (Text Encoding)

Computers store text as binary numbers using:

  • ASCII (7 bits): Represents 128 characters (0–127).
    • Example: 'A' = 65 (binary: 01000001)
  • Unicode (16/32 bits): Supports global languages (e.g., Devanagari, Chinese).

Example: Store the word "Hi" in ASCII:

'H' = 72 → 01001000
'i' = 105 → 01101001
Binary: **01001000 01101001**

NEB-style question: What is the ASCII binary code for 'N' and 'E'?


12. Applications of Number Systems

Number System Used In Advantage
Binary Computer hardware, circuits Only 0 and 1, easy for electronics
Octal Older computer systems Shorter than binary
Hexadecimal Memory addresses, programming Compact, easy to read
ASCII/Unicode Text storage and communication Standardized for global use

Real-world example:

  • RAM memory stores data in binary (e.g., 10101010).
  • CPU registers hold binary values for calculations.
  • Networking uses hex for MAC addresses (e.g., 00:1A:2B:3C:4D:5E).

Exam Tip: How to Score Full Marks in NEB Exams

  1. Conversions are key!

    • Practice converting decimal ↔ binary ↔ octal ↔ hex quickly.
    • Use the grouping method for binary to octal/hex.
  2. Binary arithmetic is tested often.

    • Master addition, subtraction (2’s complement), and multiplication.
    • Show step-by-step working (like in the examples above).
  3. Signed numbers (1’s and 2’s complement) are common.

    • Remember: 2’s complement = invert + 1.
    • For 8-bit numbers, the range is -128 to +127.
  4. ASCII/Unicode questions appear in short answers.

    • Know the binary for A-Z, a-z, and common symbols (e.g., space = 00100000).
  5. Diagrams help!

    • Draw binary grouping tables for octal/hex conversions.
    • Show 2’s complement steps clearly.
  6. NEB-style questions:

    • Short answer: Convert 45 to binary, 101101 to octal.
    • Long answer: Subtract 0101 from 1100 using 2’s complement. Explain why we use 2’s complement.
    • Application: How is 10010110 stored in memory? (Hint: It’s a binary number representing ASCII or a decimal value.)

Final Challenge: Convert 2A3 (hex) to binary, then to decimal. Now subtract 101101 (binary) from it using 2’s complement.

(Answer: 2A3 (hex) = 1010100011 (binary) = 675 (decimal). Subtraction result: 1101011 (binary) = 107 (decimal).)

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

Discussion

Loading…