DLD Lecture 3rd Week
DLD Lecture 3rd Week
CONTENTS
Why more number systems
Converting TO and From Decimal
Counting Base 2,8,10 and 16
Review Decimal to Binary and Vice Versa
Decimal to Base N and Vice Versa
Decimal to Octal and Vice Versa
Decimal to Hexadecimal and Vice Versa
Hexadecimal to Octal
Octal to Binary
Binary Octal Hexadecimal shortcut
Binary Codes(BCD and ASCII)
1
Why More Number Systems?
Why do we need more number systems?
• Humans understand decimal
• Since computers have 32, 64, and even 128 bit busses, displaying
numbers in binary is difficult to handle due to large size.
• Data on a 32 bit data bus would look like the following:
Decimal10
0123456789
Successive Weighted
Division Multiplication
Weighted Successive
Multiplication Division
Successive Weighted
Division Multiplication
Octal8 Hexadecimal16
01234567 0123456789ABCDEF
Binary2
01
3
Counting . . . 2, 8, 10, 16
Decimal Binary Octal Hexadecimal
0 0000 0 0
1 0001 1 1
2 0010 2 2
3 0011 3 3
4 0100 4 4
5 0101 5 5
6 0110 6 6
7 0111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F 4
Review: Decimal ↔ Binary
Successive
Division
a) Divide the decimal number by 2; the remainder is the LSB of the binary
number.
b) If the quotient is zero, the conversion is complete. Otherwise repeat step
(a) using the quotient as the decimal number. The new remainder is the
next most significant bit of the binary number.
Weighted
Multiplication
Successive
Division
a) Divide the decimal number by N; the remainder is the LSB of the ANY
BASE Number .
b) If the quotient is zero, the conversion is complete. Otherwise repeat step
(a) using the quotient as the decimal number. The new remainder is the
next most significant bit of the ANY BASE number.
Weighted
Multiplication
a) Multiply each bit of the ANY BASE number by its corresponding bit-
weighting factor (i.e., Bit-0→N0; Bit-1→N1; Bit-2→N2; etc).
b) Sum up all of the products in step (a) to get the decimal number.
6
Decimal ↔ Octal Conversion
The Process: Successive Division
• Divide the decimal number by 8; the remainder is the LSB of the
octal number .
• If the quotient is zero, the conversion is complete. Otherwise repeat
step (a) using the quotient as the decimal number. The new
remainder is the next most significant bit of the octal number.
Example:
Convert the decimal number 9410 into its octal equivalent.
11
8 94 r 6 LSB
1 9410 = 1368
8 11 r 3
0
7
8 1 r 1 MSB
Example: Dec → Octal
Example:
Convert the decimal number 18910 into its octal equivalent.
8
Example: Dec → Octal
Example:
Convert the decimal number 18910 into its octal equivalent.
Solution:
23
8 189 r 5 LSB
2 18910 = 2758
8 23 r 7
0
8 2 r 2 MSB
9
Octal ↔ Decimal Process
The Process: Weighted Multiplication
• Multiply each bit of the Octal Number by its corresponding bit-
weighting factor (i.e., Bit-0→80=1; Bit-1→81=8; Bit-2→82=64; etc.).
• Sum up all of the products in step (a) to get the decimal number.
Example:
Convert the octal number 1368 into its decimal equivalent.
1 3 6
82 81 80
Bit-Weighting 136 8 = 9410
64 8 1 Factors
64 + 24 + 6 = 9410
10
Example: Octal → Dec
Example:
Convert the octal number 1348 into its decimal equivalent.
11
Example: Octal → Dec
Example:
Convert the octal number 1348 into its decimal equivalent.
Solution:
1 3 4
82 81 80
64 8 1
1348 = 9210
64 + 24 + 4 = 9210
12
Decimal ↔ Hexadecimal Conversion
The Process: Successive Division
• Divide the decimal number by 16; the remainder is the LSB of the
hexadecimal number.
• If the quotient is zero, the conversion is complete. Otherwise
repeat step (a) using the quotient as the decimal number. The
new remainder is the next most significant bit of the hexadecimal
number.
Example:
Convert the decimal number 9410 into its hexadecimal equivalent.
5
16 94 r E LSB
0
16 5 r 5 MSB
9410 = 5E16
13
Example: Dec → Hex
Example:
Convert the decimal number 42910 into its hexadecimal equivalent.
14
Example: Dec → Hex
Example:
Convert the decimal number 42910 into its hexadecimal equivalent.
Solution:
26
16 429 r D (13) LSB
1
16 26 r A (10) 42910 = 1AD16 = 1ADH
0
16 1 r 1 MSB
15
Hexadecimal ↔ Decimal Process
The Process: Weighted Multiplication
• Multiply each bit of the hexadecimal number by its
corresponding bit-weighting factor (i.e., Bit-0→160=1; Bit-
1→161=16; Bit-2→162=256; etc.).
• Sum up all of the products in step (a) to get the decimal number.
Example:
Convert the octal number 5E16 into its decimal equivalent.
5 E
161 160 5E 16 = 9410
Bit-Weighting
16 1 Factors
80 + 14 = 9410
16
Example: Hex → Dec
Example:
Convert the hexadecimal number B2EH into its decimal equivalent.
17
Example: Hex → Dec
Example:
Convert the hexadecimal number B2EH into its decimal equivalent.
Solution:
B 2 E
162 161 160
256 16 1
B2EH = 286210
2816 + 32 + 14 = 286210
18
Example: Hex → Octal
Example:
Convert the hexadecimal number 5AH into its octal equivalent.
19
Example: Hex → Octal
Example:
Convert the hexadecimal number 5AH into its octal equivalent.
Solution:
First convert the hexadecimal number into its decimal equivalent,
then convert the decimal number into its octal equivalent.
11
5 A 8 90 r 2 LSB
161 160
1
16 1
8 11 r 3
0
80 + 10 = 9010 8 1 r 1 MSB
5AH = 1328 20
Example: Octal → Binary
Example:
Convert the octal number 1328 into its binary equivalent.
21
Example: Octal → Binary
Example:
Convert the octal number 1328 into its binary equivalent.
Solution:
First convert the octal number into its decimal equivalent, then
convert the decimal number into its binary equivalent.
45
1 3 2 2 90 r 0 LSB
82 81 80 22
2 45 r 1
64 8 1 11
2 22 r 0 1328 = 10110102
64 + 24 + 2 = 9010 5
2 11 r 1
2
2 5 r 1
1
2 2 r 0
0 22
2 1 r 1 MSB
Binary ↔ Octal ↔ Hex Shortcut
Because binary, octal, and hex number systems are all powers of two
(which is the reason we use them) there is a relationship that we can
exploit to make conversion easier.
1 0 1 1 0 1 0 2 = 132 8 = 5A H
To convert directly between binary and octal, group the binary bits into
sets of 3 (because 23 = 8). You may need to pad with leading zeros.
0 0 1 0 1 1 0 1 0 2= 1 3 28
5 A 0101 1 010 23
Example: Binary ↔ Octal ↔ Hex
Example:
Using the shortcut technique, convert the hexadecimal number A6 16
into its binary and octal equivalent. Use your calculator to check your
answers.
24
Example: Binary ↔ Octal ↔ Hex
Example:
Using the shortcut technique, convert the hexadecimal number A6 16
into its binary & octal equivalent. Use your calculator to check your
answers.
Solution:
First convert the hexadecimal number into binary by expanding the
hexadecimal digits into binary groups of (4).
A 6 16
A616 = 101001102
1010 0110
Convert the binary number into octal by grouping the binary bits into
groups of (3).
010100110 101001102 = 2468
25
2 4 6
Human perception
• We naturally live in a base 10 environment
• Computer exist in a base 2 environment
• So give the computer/digital system the
task of doing the conversions for us.
26
Binary Codes
• “An n-bit binary code is a group of n bits
that assume up to 2n distinct combinations
of 1s and 0s, with each combination
representing one element of the set being
coded”
29
And now
• Consider 5 + 5
• 50 1 0 1
• +5 0101
• giving 1010 which is binary 10 but
not a BCD digit!
• What to do?
• Try adding 6??
30
Adding 6
• Had 1010 and want to add 6 or 0110
– so 1010
– plus 6 0110
– Giving 10000
• Or a carry out to the next binary digit, or if
the binary in BCD, the next BCD digit.
• The answer is 0001 0000 (BCD code)
• This code is equal to decimal 10
31
Another carry example
• Add 7 + 6
– have 7 0111
– plus 6 0110
– Giving 1 1 0 1 and again out of range
– Adding 6 0110
– Giving 1 0 0 1 1 so a 1 carries out to the
next BCD digit
– FINAL BCD answer 0001 0011 or 1310
32
Multibit BCD
• Add the BCD for 417 to 195
• Would expect to get 612
– BCD setup - start with Least Significant Digit
– 0100 0001 0111
– 0001 1001 0101
– 1100
– Adding 6 0110
– Gives 10010
33
Continuing multibit
• Had a carry to the 2nd BCD digit position
– 1
– 0100 0001 done
– 0001 1001 0010
– 1011
– Again must add 6 0110
– Giving 1 0001
– And another carry
34
Still Continuing multibit
• Had a carry to the 3rd BCD digit position
– 1
– 0100 done done
– 0001 0001 0010
– 0110
– And answer is 0110 0001 0010 or the BCD
for the base 10 number 612
35
Alphanumeric Codes
• How do you handle alphanumeric data?
• Easy answer!
• Formulate a binary code to represent
characters
• For the 26 letter of the alphabet would
need 5 bits for representation.
• But what about the upper case and lower
case, and the digits, and special
characters
36
American Standard Code for
Information Interchange(ASCII)
• Represents the numbers
– All start 011 xxxx and the xxxx is the BCD for
the digit
• Represent the characters of the alphabet
– Start with either 100, 101, 110, or 111
– A few special characters are in this area
• Start with 010 – space and !”#$%&’()*+.-,/
• Start with 000 or 001 – control char like
ESC
37
ASCII Example
• Encoding of 123
– 011 0001 011 0010 011 0011
• Encoding of Joanne
– 100 1010 110 1111 110 0001
– 110 1110 110 1110 110 0101
38
What to do with the 8th Bit?
• In digital systems data is usually organized
as bytes or 8 bit of data.
• How about using the 8th bit for an error
coding. This would help during data
transmission, etc.
• Parity bit – the extra bit included to make
the total number of 1s in the byte either
even or odd – called even parity and odd
parity
39
Example of Parity
• Consider data 100 0001
– Even Parity 0100 0001
– Odd Parity 1100 0001
• Consider data 1010100
– Even Parity 1101 0100
– Odd Parity 0101 0100
• A parity code can be used for ASCII
characters and any binary data.
40