0% found this document useful (0 votes)
31 views61 pages

Lecture1B. Number System, Operation, and Codes

Uploaded by

thaohm5782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views61 pages

Lecture1B. Number System, Operation, and Codes

Uploaded by

thaohm5782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 61

Lecture 2:

Number systems, Operations, and Codes

 Number systems
 Binary Arithmetic Operations
 Binary Codes
Number system
 Decimal Numbers
 Binary Numbers
 Binary-to-Decimal Conversion
 Decimal-to-Binary Conversion
 Hexadecimal Numbers and relation with Binary
 Octal Numbers and relation with Binary
 Conversion between Hexadecimal, Octal, and Decimal
Number system
 Decimal Numbers
A decimal number can be expressed a sum of the values of
each digit.
= 5 102 + 6 101 + 8 100 + 2 10-1 +3
(568.23)10
10-2
weight weight weight ● weight weight
 The weight of a digit is a power of 10
Point
corresponding to the digit’s position.
 For whole numbers, the weights are positive powers of 10, and
it increase from right to left.
 For fractional numbers, the weights are negative powers of 10,
and it decreases from left to right.
Number system
 Decimal Numbers
In general, a decimal number D of the form d2d1d0.d-1d-2
has the value: ( 0  di  9 )
(D)10 = d2·102 + d1·101 + d0·100 + d-1·10-1 + d-2·10-2
10  base or radix of the number system
 Base-r Number system
(N) r = Kn·rn + Kn-1·rn-1 + … +K1·r1 + K0·r0 + K-1·r-1 + … +K-m·r-m
r  base ri  weight Ki  coefficient
Number system
 Base-r Number system
(N) r = Kn·rn + Kn-1·rn-1 + … +K1·r1 + K0·r0 + K-1·r-1 + … +K-m·r-m
r  base (cơ ri  weight Ki  coefficient
số ) (trọng số) (hệ số)

Usually,  r  2
 m and n are positive integers
 The digits or symbols of Ki are different
in different number systems
In decimal system r = 10 Ki = 0, 1, …, 9
The decimal system with its ten digits is
Number system
a base-10 system; the binary system with
 Binary Numbers its two digits is base-2 system.
(N) r = Kn·rn + Kn-1·rn-1 + … +K1·r1 + K0·r0 + K-1·r-1 + … +K-m·r-m

Here, r=2 Ki = 0 or 1 only two digits (bits)


(N) 2 = Kn·2n + Kn-1·2n-1 + … +K1·21 + K0·20 + K-1·2-1 + … +K-m·2-m
2 is often omitted
= (Kn Kn-1 … K1 K0 . K-1 K-m)2

Example 1011.01 The general form of a binary number

(1011.01)2 1011.012 1011.01 b or 1011.01 B


Number system The weight of MSB is most weight;
 Binary Numbers The weight of LSB is least weight.
LSB and MSB in a binary number
1001011.01001
MSB LSB
LSB MSB
For whole part The right-most bit is the Least Significant Bit
The left-most nonzero bit is the Most Significant Bit
For fractional part
The left-most bit (the right bit of point) is the Most Significant Bit
The right-most nonzero bit is the Least Significant Bit
Number system
 Binary Numbers
How to count in a binary number ?
0, 1, 10, 11, 100, 101, 110, …

With n bits The largest decimal number is 2n-1


We can count from 0 to 2n-1
Example, with 4 bits we can count from 0 to 15
Number system
 Binary-to-Decimal Conversion
Directly using base-2 expression to calculate by decimal
operation
Example :
= 123 +122 +021 +120 +02-1
(1101.01)2
+12-2
= 23 +22 +20 +2-2
= 8 +4 +1 + 0.25
= (13.25)10
Number system
 Decimal-to-Binary Conversion
1. Sum-of-Weights Method ( usually for whole number )
… 27 26 25 24 23 22 21 20 The Weights of eight bits
… 128 64 32 16 8 4 2 1 The values of Weights
Example :
(81)10 = 64 +16 +1 (every number must be a weight of binary)

= 26 +264 +20 5 (sum-of-weights)


= 12 +02 +124 +023 +022 +021
+120
= (1010001)2 (every position coefficient)
Number system Hence (81)10 = (1010001)2

 Decimal-to-Binary Conversion
2. Repeated Division-by-2 for wholes and/or Multiplication-
by-2 for fractions Method
Example : (81.65)10 = (?)2 Quotient

2 2 2 2 2 2 2
0 1 2 5 10 20 40 81
Until the
quotient Remainder
is zero
1 0 1 0 0 0 1
K6 K5 K4 K3 K2 K1 K0
( MSB ) ( LSB )
The whole binary result is composed of remainders
Number system Until the fractional product
is zero or the limited digits
 Decimal-to-Binary Conversion is reached.
2. Repeated Division-by-2 for wholes and/or Multiplication-
by-2 for fractions Method
Example : (81.65)10 = (?)2 ( Precision limited to five
bits in fractional part)
Fractional product

2 2 2 2 2 2
0.65 0.3 0.6 0.2 0.4 0.8

1 0 1 0 10 carry 1
The carry K-1 K-2 K-3 K-4 K-5 K-6
to integer
Number system
 Decimal-to-Binary Conversion
2. Repeated Division-by-2 for wholes and/or Multiplication-
by-2 for fractional Method
Example : (81.65)10 = (?)2 =(1010001.10101)2
2 2 2 2 2 2
0.65 0.3 0.6 0.2 0.4 0.8

1 0 1 0 10 1
K-1 K-2 K-3 K-4 K-5 K-6
The fractional binary result is composed of carries
So : (0.65)10=(0.10101)2 (81)10 = (1010001)2
Number system
 Hexadecimal Numbers and Relation with Binary
Base-16 system
(N) r = Kn·rn + Kn-1·rn-1 + … +K1·r1 + K0·r0 + K-1·r-1 + … +K-m·r-m

Here, r = 16
Hexadecimal numbers has sixteen coefficients, ten
numeric digits and six alphabetic characters
Ki = 0, 1, 2, …, 9, A, B, C, D, E, F
ten six
Number system
 Hexadecimal Numbers and Relation with Binary
Base-16 system
(N)16 = Kn·16n + … +K1·161 + K0·160 + K-1·16-1 + … +K-m·16-m
= (Kn … K1 K0 . K-1 K-m)16

Example 2AF.3B (2AF.3B)16 2AF.3B16


2AF.3B h or 2AF.3B H
Counting 0, 1, 2, …, 9, A, B, C, D, E, F,
1A 1C 1D
10, 11, …, 19, 1B, 1E, 1F, 20, …
, , ,
Number system
 Hexadecimal Numbers and Relation with Binary
The relation between hexadecimal and binary is very simply
Each hexadecimal digit represents a 4-bit binary number
Binary Hexadecimal Binary Hexadecimal
0000 0 1000 8
0001 1 1001 9
0010 2 1010 A
0011 3 1011 B
0100 4 1100 C
0101 5 1101 D
0110 6 1110 E
0111 7 1111 F
Number system
 Hexadecimal Numbers and Relation with Binary
Binary-to-Hexadecimal Conversion
Break the binary number into 4-bit groups from point to
both sides, and replace each 4-bit group with the
equivalent hexadecimal symbol.
Example : (111011.10101)2 = (?)16 = ( 3B.A8)16
00111011.10101 000
Supplement two Supplement three
zeros in order to
3 B . A 8 zeros in order to
get 4-bit group get 4-bit group
Number system
 Hexadecimal Numbers and Relation with Binary
Hexadecimal-to-Binary Conversion
Directly replace each hexadecimal symbol with the
corresponding four bits of binary number
Example :
(2AF.3B)16 = 0010 1010 1111 . 0011 1011
= (1010101111.00111011)2
Number system
 The excellences of Hexadecimal Number
Digital circuits understand only binary numbers, but the
expression of a binary number is usually very long so
that it is difficult to read and write.

More compact to represent binary number


Very simple conversion with binary
Number system
 Octal Numbers and Relation with Binary
Base-8 system
(N)8 = Kn·8n + … +K1·81 + K0·80 + K-1·8-1 + … +K-m·8-m
= (Kn … K1 K0 . K-1 K-m)8
Here, r = 8 Ki = 0, 1, 2, …, 7
Example 327.24 (327.24)8 327.248
327.24 o or 327.24 O
Counting 0, 1, 2, …, 7, 10, 11, …, 17, 20, …
Number system
 Octal Numbers and Relation with Binary
The relation between octal and binary is similar with hexadecimal
A 3-bit binary number corresponds to one octal digit
Binary Hexadecimal
Octal Binary Hexadecimal
0000 0 1000 8
0001 1 1001 9
0010 2 1010 A
0011 3 1011 B
0100 4 1100 C
0101 5 1101 D
0110 6 1110 E
0111 7 1111 F
Number system
 Octal Numbers and Relation with Binary
Conversion between Octal and Binary
It is similar with hexadecimal
The differentia is only that the group is 3-bit binary number

Example : (10110.011)2 = (26.3)8

(752.1)8 = (111 101 010.001)2


Number system
 Conversion between Hexadecimal, Octal, and Decimal
Hexadecimal to Decimal Using Sum-of-Weights Method
Example
(F8C.B)16 = F×162 +8×161 +C×160 +B×16-1 (Base-16)
(replaced by
= 15×16 +8×16 +12×16 +11×16
2 1 0 -1
decimal number)
(calculating by decimal
= 3840 +128 +12 + 0.6875 operation)
= (3980.6875)10
Octal to Decimal Similar to Hexadecimal
Number system
 Conversion between Hexadecimal, Octal, and Decimal
Decimal to Hexadecimal or octal
Hexadecimal
Decimal Binary
(as a bridge) Octal
Example
(46.75)10 = (101110.11)2 (to binary by division/ multiplication-by-2)

= 0010 1110 . 1100 (break into 4-bit groups)

= (2E.C)16 (replaced by hexadecimal symbol)


Number system
 Brief Summary Conversion between several
number systems
Hexadecimal
Sum-of-Weights 4-bit group

Division( or Multiplication)-by-2
Decimal Binary
Sum-of-Weights

Sum-of-Weights 3-bit group

Octal
D B H O
Number system 0 0000 0 0
1 0001 1 1
 Brief Summary Relation 2 0010 2 2
3 0011 3 3
between several number 4 0100 4 4
systems 5 0101 5 5
6 0110 6 6
7 0111 7 7
To four bits binary number, 8 1000 8 10
the relation between them is 9 1001 9 11
10 1010 A 12
shown in this table 11 1011 B 13
12 1100 C 14
13 1101 D 15
14 1110 E 16
15 1111 F 17
Number system, Operation, and Codes

 Number system
 Binary Arithmetic
 Binary Codes
Binary Arithmetic
 Binary Arithmetic
 Addition  Multiplication
 Subtraction  Division

 Binary Arithmetic Operation in Logic Device


 1’s and 2’s Complement of Binary Number
 Signed Numbers
 Arithmetic Operation with Signed Numbers
Binary Arithmetic
 Addition
The basic rules for adding binary digits :
0+0=0 0+1=1 1+0=1 1+1=10
Carry
Example : Add 1010 and 0111
1 0 1 0 (augend)
+ 0 1 1 1 (addend)
1 1
1 0 0 0 1 (sum)
Binary Arithmetic
 Subtraction
The basic rules for subtracting bits :
0-0=0 1-1=0 1-0=1 0-1= – 1 10-1=01
Example : Subtract 0111 from 1011
0111 – 1011= ? = – 0100
1011 (minuend) 1011
- 0111 (subtrahend) - 0111
0100 (difference) 0100
Binary Arithmetic
 Multiplication
The basic rules for multiplying bits :
00=0 01=0 10=0 11=1
Example : Perform the Multiplication 1010  0101
1 0 1 0 (multiplicand) 1010
 0 1 0 1 (multiplier) 0101
1010 1010
0000 (partial
+ 1010
1010 product)
+ 0000 110010
0 1 1 0 0 1 0 (product)
Binary Arithmetic
 Division
The basic rules for dividing bits :
01=0 11=1 ( Divisor can’t be zero )
Example : Perform the division 1010  0011
Quotient 1 1 . 0 1 … Until the remainder is zero
11 1010 or the precision is reached.
Divisor 11
Dividend 100
11 Remainder
100
11
1
Binary Arithmetic
 Binary Arithmetic
 Addition  Multiplication
 Subtraction  Division

 Binary Arithmetic Operation in Logic Device


 1’s and 2’s Complement of Binary Number
 Signed Numbers
 Arithmetic Operation with Signed Numbers
Binary Arithmetic Operation in Logic Device
 1’s and 2’s Complement of Binary Number
 1’s Complement
The 1’s complement of a binary number is obtained by changing
each 0 to 1 and each 1 to 0. In other words, changing each bit to
its complement. Perform 1’s complement
Example Original Number 101101 once again the number is
changed back to original
1’s Complement 010010 number
Other method: 111111 All of 1s
The same bits number which - 101101 Original Number
every bit is 1 minus the binary
010010 1’s Complement
number
Binary Arithmetic Operation in Logic Device
 1’s and 2’s Complement of Binary Number
 2’s Complement
The 2’ complement of a binary number is formed by adding 1
to the LSB of the 1’s complement
Example 1101100 Original Number
0010011 1’s Complement
+ 1 Add 1 to the LSB
0010100 2’s Complement
Perform 2’s complement once again the number is changed
back to original number
Binary Arithmetic Operation in Logic Device
 1’s and 2’s Complement of Binary Number
 2’s Complement
Other method:
Using the binary number that value is 2n minus the original
binary number. (n is the bits of the original binary)
The weight of this bit is 27
Example 10000000 Binary number of 27
- 1101100 Original Number, seven bits, n=7
0010100 2’s Complement
The third method is an alternative method ( refer to textbook page 52 )
What is the 2’s complement form of zero?
Binary Arithmetic Operation in Logic Device
 Signed Numbers
Only 0 and 1 can be recognized in a digital system
How express positive and negative number in a digital system?
In fact, by adding one bit to the left of left-most bit of binary
number to express the sign bit.
The sign bit 0 Positive 1 Negative
Example 00011001 + 0011001 10011001
Sign bit Magnitude bits
- 0011001
This form number is called sign-magnitude system
Binary Arithmetic Operation in Logic Device
 Signed Numbers
The sign bit 0 Positive 1 Negative
Example 00011001 + 0011001
(sign-magnitude)
10011001 - 0011001
Here, the magnitude bits are in true binary for both
There are three ways to represent a signed integer binary number:
• Sign-magnitude (its magnitude bits are in true binary)
• 1’s complement (be seldom used in binary arithmetic operation)

• 2’s complement (be frequently used)


Binary Arithmetic Operation in Logic Device
 Signed Numbers
 When the number is positive
The forms of a number’s sign-magnitude, 1’s complement
and 2’s complement are same
Sign bit

Example 00011001 (sign-magnitude)


(Positive) 00011001 (1’s complement)
00011001 (2’s complement)
+25 (equivalent decimal number)
Binary Arithmetic Operation in Logic Device
 Signed Numbers
 When the number is negative
The forms of a number’s sign-magnitude, 1’s complement
and 2’s complement are different
Sign bit magnitude bits are in true

Example 10011001 (sign-magnitude)


(Negative) 11100110 (1’s complement)
11100111 (2’s complement)
-25 (equivalent decimal number)
Binary Arithmetic Operation in Logic Device
 Signed Numbers
Example: Determine the decimal values of the following signed
binary numbers expressed in 2’s complement
(a) 01000110 (b) 11000110 (c) 11000111
Solution: (a) 01000110 = +1000110 = +(26 +22 +21) = +70
(b) 11000110 = -1000110 (magnitude in 2’s complement)
sign bit
= -0111010 (magnitude in true)
= -(25 +24 +23 + 21) (sum-of-weights)

=-58
Binary Arithmetic Operation in Logic Device
 Signed Numbers
Example: Determine the decimal values of the following signed
binary numbers expressed in 2’s complement
(a) 01000110 (b) 11000110 (c) 11000111
Another method: directly calculate the sum-of-weights
Solution: (b) 11000110 = -27 +26 +22 +21
= -128 +64 +4 +2
The weight of sign bit is negative
= -58
If the sign bit is 0, ignoring its value
Binary Arithmetic Operation in Logic Device
 Arithmetic Operation with Signed Numbers
(Only discussing the whole binary numbers represented by 2’s complement form)
 Addition
Add the two numbers and discard any final carry bit
Example Find the result of the sign numbers:
Sign bit 10011001+00100001 11011001+10110001
10011001 (augend) 11011001
+ 00100001 (addend) Discard final + 10110001
carry 1 10001010
10111010 (sum)
-70 (equivalent decimal) -118
Binary Arithmetic Operation in Logic Device
 Addition with Signed Numbers
A danger of overflow Avoid it by extending magnitude bits
Condition: The sum exceeds the range that can be represented
by the appointed magnitude bits
Example 10011001+10110001
For seven magnitude bits
10011001 -103
Max: 01111111 +127
Discard final + 10110001 + -79
carry Min : 10000000 -128
1 01001010 -182
Out of range
Sign bit +74
Both sign and magnitude are incorrect due to overflow
How to solve this problem ?
Binary Arithmetic Operation in Logic Device
 Arithmetic Operation with Signed Numbers
 Subtraction
Take the 2’s complement of the subtrahend (include sign
bit) and add, moreover discard any final carry bit
Example 11011001-10110001 10011001-00100001
-39 -(-79)=+40
11011001 (Minuend)
+ 01001111 (2’s complement of subtrahend)
Discard final carry
1 00101000 (Difference)
+40 (equivalent decimal)
Binary Arithmetic Operation in Logic Device
 Arithmetic Operation with Signed Numbers
 Subtraction
Take the 2’s complement of the subtrahend (include sign
bit) and add, moreover discard any final carry bit
Example 11011001-10110001 10011001-00100001
-39 -(-79)=+40 -103 -33 = -136
10011001
+ 11011111
1 01111000
+120
The result is incorrect due to overflow
Binary Arithmetic Operation in Logic Device
 Arithmetic Operation with Signed Numbers
 Multiplication and division with Signed Numbers

Multiplication is generally accomplished using addition.


Division is generally accomplished using subtraction.

We don’t discuss them in detail


Number system, Operation, and Codes

 Number system
 Binary Arithmetic
 Binary Codes
Binary Codes

 The 8421 Code (BCD)


 The Excess-3 code
 The Gray Code
 ASCII Code
 Error-Detecting Code
An assigned some bits as a group to represent some digits,
letters of the alphabet, symbols, and other information.
Binary Codes
 The 8421 code
 It is a type of binary coded decimal (BCD) code.
 BCD mines that each decimal digit, 0 through 9, is represented by
binary code of four bits.
 The 8421 indicates the binary weights of the four bits (23, 22, 21,
20).
 The 8421 code is the predominant BCD code, so when we say BCD
code, it usually means the 8421 code unless otherwise stated.
 The six code combinations (1010, 1011, 1100, 1101, 1110, 1111) are
invalid in BCD
Binary Codes
Decimal BCD
 The 8421 code digit code
0 0000
 Convert decimal number to BCD 1 0001
Example (276.8)10 = ( ? )BCD 2 0010
3 0011
2 7 6 . 8 4 0100
5 0101
6 0110
0010 0111 0110.1000 7 0111
8 1000
 Convert BCD to decimal number 9 1001
1010
Example (101101001.01)BCD = (?)10 1011
1100
000 101101001.0100 Invalid 1101
1110
codes 1111
1 6 9 . 4
Binary Codes
 BDC Addition
Step 1: add the two BCD numbers, using the rules for
binary addition.
Step 2: if a 4-bit sum is equal to or less than 9, it is a valid
BCD number.
Step 3: if a 4-bit sum is greater than 9, or if a carry out of
the 4-bit group is generated, it is an invalid result. Add
6 (0110) to the sum in order to skip the six invalid
states and return the code to 8421.
Binary Codes
 BDC Addition
Example
Add the BCD numbers: 00010110 + 00010101
0001 0110
Verify Right group is invalid
16 + 0001 0101 (>9), left group is valid.
+ 15 0010 1011
0110
+ 00111 0001
31 Add 6 to invalid code. Add
carry, 0001, to next group.

Valid BCD number


Binary Codes
 BDC Addition
Example
Add the BCD numbers: 10010010 + 01011000
Verify
92
+ 58

150
Binary Codes
 The Excess-3 code Decimal
digit
Excess-3
code
0000
 It is another type of BCD code. 0001
0010
 It is unweighted code. 0 0011
1 0100
 There are also six invalid codes 2 0101
3 0110
(0000, 0001, 0010, 1101, 1110, 4 0 1 1 1 Invalid
1111) in Excess-3 code 5 1 0 0 0 codes
6 1001
7 1010
8 1011
9 1100
1101
1110
1111
Binary Codes
 The Gray code Decimal Binary
4-bit Gray
code
 It is unweighted code and is not 0 0000 0000
1 0001 0001
arithmetic code. 2 0010 0011
 There is only a single bit difference 3 0011 0010
4 0100 0110
between the adjacent Gray codes. 5 0101 0111
6 0110 0101
 Like binary numbers, the Gray code 7 0111 0100
8 1000 1100
can have any number of bits. 9 1001 1101
 It usually represent some sequence 10 1010 1111
11 1011 1110
information 12 1100 1010
 Conversion between binary and 13 1101 1011
14 1110 1001
Gray code conveniently (refer to 15 1111 1000
textbook page 77)
Binary Codes
Excess 4-bit
Dec Binary BCD
 The relationship between 0 0000 0000
-3
0011
Gray
0000
several typical codes 1
2
0001
0010
0001
0010
0100
0101
0001
0011
3 0011 0011 0110 0010
Note: 4 0100 0100 0111 0110
5 0101 0101 1000 0111
“xxxx” represents “don’t care” 6 0110 0110 1001 0101
7 0111 0111 1010 0100
terms. It means these states are 8 1000 1000 1011 1100
invalid or never occur. 9 1001 1001 1100 1101
10 1010 xxxx xxxx 1111
11 1011 xxxx xxxx 1110
12 1100 xxxx xxxx 1010
13 1101 xxxx xxxx 1011
14 1110 xxxx xxxx 1001
15 1111 xxxx xxxx 1000
Binary Codes
 ASCII Code
(American Standard Code for Information Interchange)

The ASCII code is a seven-bit code, so it has 128


possible code groups to represent letters of the
alphabet, punctuation marks, and other special
characters as well as number.

About more detail, refer to textbook page 79-83


Binary Codes
 Error-Detecting Code
 A frequent used error-detecting code is formed by
attaching a parity bit to a group of bits in order to make
the total number of 1s in a group always even or always
odd
 A given system operates with even or odd parity, but not
both
 The parity bit can be attached to the code at either the
beginning or the end
Binary Codes
 Error-Detecting Code
Example
Assign the proper even parity bit at the beginning to the
code groups: 101101, 1000110
Make total number of 1s is always even
parity bit
Attach even parity bit 0
101101 0101101
parity bit
Attach even parity bit 1
1000110 11000110
Binary Codes
 Error-Detecting Code Can’t detect all of errors

How to detect error using this code?


Example: The block diagram of a communication system is shown in fig.
Assume that we wish to transmit the code 1000110 by it.

1000110 Even parity 11000110 Spread by


Sender
former electromagnetic
Bit error wave

Appear error Error 0


11000100
Receiver
detector
even number of 1s does not appear

{end}

You might also like