Chapter two
Numbering system
Number system, operations and codes (2 lecture hour)
Decimal number
Binary conversation
Hexadecimal number
Octal number
1’s and 2’s Binary number
Decimal to compliment of binary number
Signed number
BCD
Decimal Number System
• It is a system we use in everyday life.
• Also known as the Base-10 system.
• It consists of 10 distinct digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
• The value of each digit depends on its position in the number.
Positional Weight of Decimal System
• Each position in a number represents a power of 10.
• Starting from the rightmost digit (Least Significant Digit).
• Example: The number 4537
• 4 5 3 7
• ↓ ↓ ↓ ↓
• 10³ 10² 10¹ 10⁰ (Positional Weights)
• 1000 100 10 1
• (4 x 1000) + (5 x 100) + (3 x 10) + (7 x 1) = 4000 + 500 + 30 + 7 = 4537
Why Do We Need Other Number Systems?
• Digital Electronics: Computers are built from transistors, which are
essentially switches that are either ON (1) or OFF (0).
• The Binary (Base-2) system, with only two states, is a natural fit.
• Hexadecimal and Octal systems are used as convenient "shorthand" for
representing long binary numbers for human readability.
Binary Number System
• It is a language of computers.
• A Base-2 system.
• It consists of 2 distinct digits: 0 and 1.
• Each digit is called a bit (Binary Digit).
• A group of 8 bits is called a byte.
Positional Weight of Binary System
• Each position in a binary number represents a power of 2.
• Example: The binary number 1101₂
• 1 1 0 1
• ↓ ↓ ↓ ↓
• 2³ 2² 2¹ 2⁰ (Positional Weights)
• 8 4 2 1
• (1 x 8) + (1 x 4) + (0 x 2) + (1 x 1) = 8 + 4 + 0 + 1 = 13₁₀
Binary to Decimal Conversion
• Convert the following binary numbers to decimal:
• 1010₂
• 1111₂
• 1001 0110₂
Continue...
• 1010₂ = (1x8)+(0x4)+(1x2)+(0x1) = 10₁₀
• 1111₂ = (1x8)+(1x4)+(1x2)+(1x1) = 15₁₀
• 1001 0110₂ = (1x128)+(0x64)+(0x32)+(1x16)+(0x8)+(1x4)+(1x2)+(0x1)
= 150₁₀
• We can see in to two ways two convert Binary to Decimal Conversion.
Decimal to Binary Conversion: Method 1
Sum-of-Weights Method
• Find the largest power of 2 that is less than or equal to the decimal
number.
• Place a 1 in that position.
• Subtract that power from the number.
• Repeat until the remainder is 0.
• Fill all other positions with 0.
Example: Convert 25₁₀ to Binary.
• Largest power of 2 ≤ 25 is 16 (2⁴). → Bit 4 = 1. Remainder: 25-16=9.
• Largest power of 2 ≤ 9 is 8 (2³). → Bit 3 = 1. Remainder: 9-8=1.
• Largest power of 2 ≤ 1 is 1 (2⁰). → Bit 0 = 1.
• Bits 2 and 1 are 0.
• Result: 1 1 0 0 1₂ (16 + 8 + 1 = 25)
Decimal to Binary Conversion: Method 2
Repeated Division-by-2 Method
• Divide the decimal number by 2.
• Record the remainder (this will be the Least Significant Bit, LSB).
• Use the quotient for the next division.
• Repeat until the quotient is 0.
• The binary number is the remainders read from bottom to top.
Example Convert 29₁₀ to Binary
• Operation | Quotient | Remainder
• 29 ÷ 2 | 14 | 1 ↑ (LSB)
• 14 ÷ 2 | 7 | 0 ↑
• 7÷2 | 3 | 1 ↑
• 3÷2 | 1 | 1 ↑
• 1 ÷ 2 | 0 | 1 ↑ (MSB)
• Reading remainders from bottom to top: 29₁₀ = 11101₂
Continue…
• Convert the following decimal numbers to binary:
• 42₁₀
• 67₁₀
• 129₁₀
Continue…
• Let’s take a number 42₁₀, 67₁₀ , 129₁₀
42 ÷ 2 = 21, R= 0 1 ÷ 2 = 0, R=1
21 ÷ 2 = 10 , R=1 Result: 101010₂
10 ÷ 2 = 5 , R=0 The same sequence 67₁₀ = 1000011₂
5 ÷ 2 = 2, R=1 The same sequence 129₁₀ = 10000001₂
2 ÷ 2 = 1, R=0
Hexadecimal Number System
• A Base-16 system.
• Used as a compact, human-readable form for binary.
• Consists of 16 digits:
• 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Where A=10, B=11, C=12,
D=13, E=14, F=15.
Hexadecimal Positional Weight
• Each position represents a power of 16.
• Example: 2A5F₁₆
• 2 A 5 F
• ↓ ↓ ↓ ↓
• 16³ 16² 16¹ 16⁰
• 4096 256 16 1
• (2 x 4096) + (10 x 256) + (5 x 16) + (15 x 1) = 10847₁₀
Binary to Hexadecimal Conversion
• Group the binary number into 4-bit nibbles, starting from the right (LSB).
• Convert each 4-bit group into its corresponding hexadecimal digit.
• Example: Convert 1101 1010 0111₂ to Hex.
• 1101 = D₁₆
• 1010 = A₁₆
• 0111 = 7₁₆
• Result: DA7₁₆
Hexadecimal to Binary Conversion
• Convert each hexadecimal digit into its 4-bit binary equivalent.
• Example: Convert 3E9₁₆ to Binary.
• 3 = 0011
• E = 1110
• 9 = 1001
• Result: 0011 1110 1001₂ or 1111101001₂
Decimal to Hexadecimal Conversion
• Repeated Division-by-16 Method
• Divide the decimal number by 16.
• Record the remainder (convert 10-15 to A-F).
• Use the quotient for the next division.
• Repeat until quotient is 0.
• The hex number is the remainders read from bottom to top.
Example
• Convert 2549₁₀ to Hexadecimal.
• Operation | Quotient | Remainder
• 2549 ÷ 16 | 159 | 5 ↑
• 159 ÷ 16 | 9 | 15 (F) ↑
• 9 ÷ 16 | 0 | 9 ↑
• Reading remainders from bottom to top: 2549₁₀ = 9F5₁₆
Hexadecimal Arithmetic Addition
• Add digit by digit, If the sum is 16 or more, subtract 16 and carry 1 to the
next digit.
• 1A7
• + 2 B 9 =4 6 0
• 7+9=16 → 0, carry 1.
• 1+A+B = 1+10+11=22 → 22-16=6, carry 1.
• 1+1+2=4.
Why is Hexadecimal So Useful?
• Compactness: FF is much easier to read/write than 11111111.
• Easy Conversion: Every hex digit corresponds to exactly 4 bits.
• Memory Addresses: Widely used to represent memory addresses.
• Color Codes: Used in web design (e.g., #FF5733).
Octal Number System
• A Base-8 system.
• Also used as a shorthand for binary, though less common than Hex today.
• Consists of 8 digits: 0, 1, 2, 3, 4, 5, 6, 7.
Octal Positional Weight
• Each position represents a power of 8.
• Example: 457₈
• 4 5 7
• ↓ ↓ ↓
• 8² 8¹ 8⁰
• 64 8 1
• (4 x 64) + (5 x 8) + (7 x 1) = 256 + 40 + 7 = 303₁₀
Binary to Octal Conversion
• Group the binary number into 3-bit groups, starting from the right (LSB).
• Convert each 3-bit group into its corresponding octal digit.
• Example: Convert 101 110 011₂ to Octal.
• 101 = 5₈
• 110 = 6₈
• 011 = 3₈
• Result: 563₈
Octal to Binary Conversion
• Convert each octal digit into its 3-bit binary equivalent.
• Example: Convert 742₈ to Binary.
• 7 = 111
• 4 = 100
• 2 = 010
• Result: 111 100 010₂
Binary Arithmetic Addition
Follows four basic rules:
• 0+0=0
• 0+1=1
• 1+0=1
• 1 + 1 = 0, with a carry of 1 (since 1+1=2, which is 10₂ in binary)
Examples
• binary 1 0 1 1 in decimal (11₁₀)
• (+) binary 0 1 0 1 in decimal (5₁₀)
• Binary 1 0 0 0 0 in decimal (16₁₀)
• 1+1=0, carry 1.
• 1+0+0=1, carry 0.
• 0+1+1=0, carry 1.
• 1+0+0=1, carry 0.
Binary Arithmetic Subtraction
• Follows four basic rules:
• 0-0=0
• 1-0=1
• 1-1=0
• 0 - 1 = 1, with a borrow of 1 from the next higher significant bit.
Example
• Binary 1 0 1 0 in decimal (10₁₀)
• (- ) Binary 0 1 0 1 in decimal (5₁₀)
• Binary 0 1 0 1 in decimal (5₁₀)
• (Column 0) 0-1: Borrow needed. It becomes 10-1=1.
• (Column 1) After borrow, 0-0=0.
• (Column 2) 0-1: Borrow needed. It becomes 10-1=1.
• (Column 3) After borrow, 0-0=0.
The Problem with Direct Subtraction
• Borrowing logic is complex to implement in digital hardware.
• Is there a better way?
• Yes! We can perform subtraction using addition by using complements.
• This is a fundamental concept in computer architecture.
Complements of Binary Numbers
• Two types of complements are used:
• 1's Complement
• 2's Complement
• 2's Complement is the most important and widely used method in modern
computing for representing signed numbers and performing subtraction.
1's Complement
• The 1's complement of a binary number is obtained by changing all 1s to
0s and all 0s to 1s.
• It is simply the bitwise NOT operation.
• Examples: Binary: 1 0 1 1 0
• 1's Comp: 01001
• Binary: 001011
• 1's Comp: 110100
2's Complement
• The 2's complement of a binary number is obtained by:
• Step 1: Find the 1's complement (flip all the bits).
• Step 2: Add 1 to the least significant bit (LSB) of the 1's complement.
Example 1
• Find the 2's complement of 10110₂.
• Step 1: Find 1's complement (flip the bits).
• 10110 → 01001
• Step 2: Add 1 to the LSB.
• 01001
•+1
• 01010
Example 2
• Find the 2's complement of 10010011₂.
• 1's complement → 01101100
• Add 1 → 01101100 + 1 = 01101101
• Result: 01101101
Find 1's and 2's Complement
• Find the 1's and 2's complement for:
• 1100 1010
• 0111 0101
Continue…
• For a given binary number :-1100 1010
• 1's Comp: 0011 0101
• 2's Comp: 0011 0101 + 1 = 0011 0110
• For a given binary number :- 0111 0101
• 1's Comp: 1000 1010
• 2's Comp: 1000 1010 + 1 = 1000 1011
Subtraction Using Complements
• We can perform subtraction (A - B) by adding A to the 2's complement of
B.
The general procedure:
• Find the 2's complement of the subtrahend (B).
• Add this complement to the minuend (A).
• If there is a carry beyond the MSB, discard it.
• If there is no carry, the result is negative and in its 2's complement form.
Subtraction using 2's Complement: Case 1
(Positive Result)
• Perform 9 - 5 or (1001 - 0101) using 2's complement.
• Step 1: 2's complement of 5 (0101) is 1011.
• Step 2: Add 9 (1001) to the 2's complement of 5 (1011).
• 1 0 0 1 (9)
• + 1 0 1 1 (2's comp of 5)
• 10100
• Step 3: Discard the end carry. The remaining bits are 0100 and the Result is 0100₂ = 4₁₀
Subtraction using 2's Complement: Case 2
(Negative Result)
• Perform 5 - 9 (0101 - 1001) using 2's complement.
• Step 1: 2's complement of 9 (1001) is 0111.
• Step 2: Add 5 (0101) to the 2's complement of 9 (0111).
• 0 1 0 1 (5)
• + 0 1 1 1 (2's comp of 9)
• 1100
Continue…
• Step 3: There is no end carry.
• This means the result is negative. To find the magnitude, take the 2's
complement of the result.
• 2's complement of 1100 is 0100 (which is 4).
• Result: -4₁₀
Why Does This Work? (A Mathematical
View)
• The 2's complement of an n-bit number N is defined as 2ⁿ - N.
• So, A - B can be written as:
• A - B = A + (2ⁿ - B) = 2ⁿ + (A - B)
• If A ≥ B, the result is 2ⁿ + (A-B). The 2ⁿ represents the carry, which we
discard, leaving (A-B).
• If A < B, the result is 2ⁿ - (B-A), which is exactly the 2's complement form
of (B-A).
Signed Number Representation
• How do we represent positive and negative numbers in a computer?
Three common methods:
• Sign-Magnitude
• 1's Complement
• 2's Complement (The Modern Standard)
Sign-Magnitude Representation
• The Left-Most Bit (MSB) is the sign bit.
• 0, for positive and 1, for negative
• The remaining bits represent the magnitude (absolute value).
Example (8-bit):
• +25 = 0001 1001 (Sign bit=0, Magnitude=25)
• -25 = 1001 1001 (Sign bit=1, Magnitude=25)
• Problem: Has two representations for zero (00000000 = +0 and 10000000 = -0).
Inefficient.
2's Complement Representation (The
Standard)
• Positive numbers are represented as themselves.
• Negative numbers are represented by the 2's complement of their positive
counterpart.
• Example (8-bit):
• +25 = 0001 1001
• -25 = 1110 0111 (2's complement of 0001 1001)
• Key Advantage: Only one representation for zero (00000000). This simplifies logic
circuits.
Range of Signed Numbers
• For an n-bit number using 2's complement:
• Largest Positive Number: 2^(n-1) - 1
• Smallest Negative Number: -2^(n-1)
• Examples:
• 8-bit: Range is -128 to +127.
• 16-bit: Range is -32,768 to +32,767.
1's Complement Representation
• Positive numbers are represented as themselves.
• Negative numbers are represented by the 1's complement of their positive
counterpart.
• Example (8-bit):
• +25 = 0001 1001
• -25 = 1110 0110 (1's complement of 0001 1001)
• Problem: Also has two representations for zero (00000000 = +0 and 11111111 = -0).
Binary Coded Decimal (BCD)
• BCD is a way to represent each decimal digit with a 4-bit binary code.
• It is not a number system like binary or hex; it is a code.
• Rule: Each decimal digit (0-9) is represented by its 4-bit binary equivalent.
Example
• Convert 497₁₀ to BCD, Take each decimal digit and write its 4-bit binary value.
• 4 = 0100
• 9 = 1001
• 7 = 0111
• Result: 497₁₀ = 0100 1001 0111 (BCD)
• Compare to True Binary:
• 497₁₀ = 111110001₂
• BCD is different and often less efficient in terms of bit usage.
BCD vs. Pure Binary
Advantage of BCD
• Very easy to convert to and from decimal.
• Accurate representation and rounding for decimal numbers (important in
financial calculations).
Disadvantage of BCD
• Inefficient storage (e.g., 497 requires 12 bits in BCD, but only 9 bits in pure
binary).
• Arithmetic operations are more complex.
BCD Addition
• Add BCD digits as normal binary numbers.
• Correction Rule: If the sum for a digit group is greater than 9 (or a carry is
generated), add 0110 (6) to that group to correct it.
Example 1 (No Correction)
• Add 12 and 24 in BCD.
• 12 = 0001 0010
• 24 = 0010 0100
• 0001 0010 (12)
• + 0010 0100 (24)
• 0011 0110 (36)
Continue…
• Each digit group (0011=3, 0110=6) is valid (≤9). No correction needed.
• Result: 0011 0110 (BCD) = 36₁₀
BCD Addition: Example 2 (With Correction)
• Add 28 and 39 in BCD.
• 28 = 0010 1000
• 39 = 0011 1001
• 0010 1000 (28)
• + 0011 1001 (39)
• 0110 0001 (Binary sum = 61, invalid BCD)
Continue…
• The first digit group 0110 (6) is valid.
• The second digit group 0001 (1) is valid, but wait! The binary sum of the
lower nibble was 1000+1001=10001 (17), which generated a carry into the
upper nibble. This is the key indicator for correction.
• Add 0110 to the lower nibble: 0110 0001 + 0000 0110 = 0110 0111
• Result: 0110 0111 (BCD) = 67₁₀