Chapter 1 - Number Systems PDF
Chapter 1 - Number Systems PDF
Chapter 01
1 Number systems
Dr. Iness NEDJI MILAT
[email protected]
Computers are built around the idea of two states : true (1) and false (0).
The 2 carries the most weight; it is referred to as the most significant digit (msd).The 5
carries the least weight and is called the least significant digit (lsd).
1101
Positional value
Weights: 2x 23 2 2 21 2 0
= 0x 856
9
Counting . . . 2, 8, 10, 16
Decimal Binary Octal Hexadecimal
0 0 0 0
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 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
16 10000 20 10
10 Word vs Byte vs Nibble?
Word is a larger group of bits, typically 16, 32, or 64 bits. The size of a word
depends on the computer architecture,
Word (32 bits)
10100001000001011110101101001101
msb lsb
Decimal10
0123456789
Successive Weighted
Division Multiplication
Weighted Successive
Multiplication Division
Successive Weighted
Division Multiplication
Octal8 Hexadecimal16
01234567 0123456789ABCDEF
Binary2
01
14 From base X to Decimal Conversion
(11010) 2 = 1 * 24 + 1 * 23 + 0 * 22 + 1 * 21 + 0 * 20 = (26)10
= 0.1100112 (approximately)
17 Decimal to Octal/Hex Conversion
A decimal can be converted to octal (resp. hexadecimal) by using the same
repeated-division method that used in binary conversion, but with a division factor
of 8 (resp. 16) instead 2,
Example
266 8 423 16
33 8 26 16
2 7
4 8 1 16
1 10
0 1 0
4
18 Binary and Octal conversions
Octal to Binary Binary to Octal
Replace each octal digit with its Make groups of 3 bits, starting from
equivalent 3-bit binary sequence. the binary point.
Example: Add 0s to the ends of the number if
needed.
Convert each bit group to its
261.358 = 2 6 1 . 3 58
corresponding octal digit.
= 010 110 001 . 011 1012
Example :
261.3516 = 2 6 1 . 3 516
Convert each bit group to its
= 0010 0110 0001 . 0011 01012
corresponding hex digit.
Example :
10110100.0010112 = 1011 0100. 0010 11002
= B 4 . 2 C16
Hex Binary Hex Binary Hex Binary Hex Binary
0 0000 4 0100 8 1000 C 1100
1 0001 5 0101 9 1001 D 1101
2 0010 6 0110 A 1010 E 1110
3 0011 7 0111 B 1011 F 1111
20 Computer arithmetic
21 Types of decimal numbers
Computer arithmetic is commonly performed on two very different types of
numbers: integer and real numbers.
Unsigned : 0, 1, 2, 3,,,,
Integer
Signed : ....,-2, -1, 0, +1, +2,....
Decimal number
Range of values :
In digital computers, the range of unsigned numbers that can be represented is based
on the number of bits available in the hardware structures that store and process
information.
What is the total range of unsigned decimal values that can be represented in a single
byte (8 bits)?
Minimum number = 0000 0000 = 0
Maximum number = 1111 1111 = 255
Method 1: 1*27 + 1*26 + 1*25 + 1*24 + 1*23+ 1*22 + 1*21 + 1*20 = 255
Method 2: 28 – 1 = 255
The range of values for n bits is [0, 2n – 1]
23 Binary Addition
The elementary operations are:
0 0 1 1 1
1
+ 0 + 1 + 0 + 1 + 1
0 1 1 10 11
Compute 103+ 85 Carry in
1 1 1 1 1 1 1
103 ⎯→ 0 1 1 0 0 1 1 1 1 1 1 1
+ 0 1 0 1 0 1 0 1 +
+ 85 ⎯→ 0 1 0 1
188 ⎯→ 1 0 1 1 1 1 0 0 1 0 1 0 0
Carry out
24 Binary Multiplication
Multiplication involves the generation of partial products that are summed to
produce the final product.
The partial products are easily defined. When the multiplier bit is 1, the partial
product is the multiplicand. When the multiplier bit is 0, the partial product is 0.
1011 x (1101)
1011 x (1000 + 100 + 0 + 1) x1011 Multiplicand
1011 x (1*23 + 1*22 + 0*21 + 1*20 ) x1101 Multiplier
18 ⎯→ 0 0 0 1 0 0 1 0
27 Binary Substraction
How does the computer subtract two binary numbers?
5–3=?
0101 – 0011 = ? Materially, the sub circuit does not exist
5 + (-3) = ?
5 +? =?
Unsigned : 0, 1, 2, 3,…
(139)10=(10001011)2
Integer
Negative (-139)10=(-10001011)2
Signed : ..,-2, -1, 0, +1, +2,..
Positive (+139)10=(+10001011)2
Sign/Magnitude
One’s complement
Two’s complement
Range of values
(+2)10 0 0 1 0
+ + 1 1 0 1
(- 5)10 ⎯→
0000 0
0001 +1
0010 +2
0011 +3
0100 +4
0101 +5
0110 +6
0111 +7
1000 -0
1001 -1
1010 -2
1011 -3
1100 -4
1101 -5
1110 -6
1111 -7
Range of values
The difference concerns the negative numbers that complement the positive
numbers
Problem
Two zero’s still: + 010 (00002) different than - 010 (11112)
Range of values
One’s complement [- (2n-1 – 1), 2n-1 - 1]
32
Binary 1’c Decimal
0000 0
0001 +1
0010 +2
0011 +3
0100 +4
0101 +5
0110 +6
0111 +7
1000 -7
1001 -6
1010 -5
1011 -4
1100 -3
1101 -2
1110 -1
1111 -0
Range of values
33 Two’s complement [- 2n-1, 2n-1 - 1]
The 2’s complement system for representing signed numbers works like this:
If the number is positive, the magnitude is represented in its true binary form (unsigned), and a
sign bit of 0 is placed in front of the msb.
+ 45 = 0 1 0 1 1 0 1
If the number is negative, apply the following steps (2’c) : - 45 = ?
1. Obtain the bit pattern of absolute value (unsigned), 1 0 1 1 0 1 = 45
2. Check if the negative number can be represented 0 1 0 1 1 0 1
on the absolute value size (n=6 bits : range = [-32, 31])
otherwise insert 0 on the left (-45 ⸦ [-32, 31])
3. Invert the bit pattern (1’s complement),
1 0 1 0 0 1 0
+ 1
4. Add one to it.
1 0 1 0 0 1 1 = - 45
34 Two’s complement : examples
Add 0 on left because -9 needs 5 bits
❑ (-9)10 = (?)2 represented in 5 bits
❑ (10111)2 = (?)10
(10111)2 = - ( 2’s complement (10111))
negative
= - ( 01000 + 1)
= - ( 01001)
= - 910
❑ (00111)2 = (?)10
(00111)2 = + (1*22 + 1*21 + 1*20 )
positive
= + 710
35 4 bits Two’s complement signed integer
Binary 2’c Decimal
39
Sometimes the addition result may be larger than can be held in given word
size (It exceeds the maximum value that can be represented in n bits),
This case is called overflow (Problem).
Example 1 : For 4-bit representation 0111 = +7
+
1000 in two’s complement is - 8, not +8 0001 = +1
1000 = -X
8 WRONG
Example 2: For 8-bit representation
Magnitude of the
- 13.25 = - 0.1325 x 10+2 Exponent
For a normalized floating point number (according to the IEEE 754 standard):
S E F = f1 f2 f 3 f4 …
They are implicit
(ie, the same for
S = 0 is positive, S = 1 is negative all numbers)
Mantissa (Significand) is equal to (1.F)2 = (1.f1f2f3f4…)2
IEEE 754 assumes hidden 1. (not stored) for normalized numbers
44 Conversion to floating-point representation
We mimic the decimal floating point notation to create the binary floating point
number:
1. We first convert the real number to its binary form. We use a “binary point” to
separate integer part from fractional part of the number to make a fixed point
notation:
Ex: -13.25 = (-1101.01)2
2. We then “float” the binary point (normalization to the binary floating point notation):
- 1101.01 => -(1.10101)2 x 23 (Sign = 1, mantissa = 1.10101, exponent_value = +3)
3. Now we have to express this number without the extra symbols ( -, x, 2, point )
by convention, we divide the available bits into three fields: sign, exponent, fraction
Double precision
The exponent field is 11 bits
E = 0 and E = 2047 are reserved for special use
E = 1 to 2046 are used for normalized floating point numbers
Bias = 1023 (half of 2046)
Exponent value = E – 1023Range: -1022 to +1023
48 Summary of IEEE 754 Encoding
Single-Precision Exponent = 8 Fraction = 23 Value
Normalized Number 1 to 254 Anything ± (1.F)2 × 2E – 127
Denormalized Number 0 nonzero ± (0.F)2 × 2–126
Zero 0 0 ±0
Infinity 255 0 ±∞
NaN (not a Number) 255 nonzero NaN
10111110001000000000000000000000
Solution:
Sign = 1 is negative
E = (01111100)2 = 124, Exponent-value = E – bias = 124 – 127 = –3
Significand = (1.0100 … 0)2 = 1*20 + 1*2-2 = 1.25 (1. is implicit)
Value in decimal = - 1.01 x 2–3 = - (0.00101)2 = - (2–3 + 2–5) = – 0.15625
= - 1.25 × 2–3 = – 0.15625
Solution:
Value of exponent = (10000000101)2 – Bias = 1029 – 1023 = 6
Value of double = (1.00101010 … 0)2 × 26 (1. is implicit)
= (1001010.10 … 0)2 = 74.5
1 1 1 1 1 1 1 1 1 1
67 ⎯→ 0 1 1 0 0 1 1 1 36 ⎯→ 0 0 1 1 0 1 1 0
+ +
+ 21 0 0 1 0 0 0 0 1 + 57 ⎯→ 0 1 0 1 0 1 1 1
⎯→
1 1
1 0 0 0 1 0 0 0 93 1 0 0 0 1 1 0 1
88
0 1 1 0
1 0 0 1 0 0 1 1
55 Gray code
The Gray code is a binary code used in applications where numbers
change rapidly (Ex. : position sensors in the mouse).
In Gray code, only one bit changes from each value to the next.
This allows to avoid problems in systems where an error can occur if more
than one bit changes at a time
Binary 1 1 0 1 0 1
msb
Gray 1
msb
57 Binary to Gray code
Same → 0
(110101) 2 = (?)Gray Different → 1
Binary 1 1 0 1 0 1
msb
Gray 1 0
msb
58 Binary to Gray code
Same → 0
(110101) 2 = (?)Gray Different → 1
Binary 1 1 0 1 0 1
msb
Gray 1 0 1
msb
59 Binary to Gray code
Same → 0
Different → 1
(110101) 2 = (?)Gray
Binary 1 1 0 1 0 1
msb
Gray 1 0 1 1
msb
60 Binary to Gray code
Same → 0
(110101) 2 = (?)Gray Different → 1
Binary 1 1 0 1 0 1
msb
Gray 1 0 1 1 1
msb
61 Binary to Gray code
Same → 0
(110101) 2 = (?)Gray Different → 1
Binary 1 1 0 1 0 1
msb
Gray 1 0 1 1 1 1
msb
(110101) 2= (101111)Gray
62 Gray to Binary
(101111) Gray = (?) 2
Gray 1 0 1 1 1 1
msb
Binary 1
msb
63 Gray to Binary
(101111) Gray = (?) 2
Gray 1 0 1 1 1 1
msb
Same → 0
Different → 1
Binary 1 1
msb
64 Gray to Binary
(101111) Gray = (?) 2
Gray 1 0 1 1 1 1
msb
Same → 0
Different → 1
Binary 11 0
msb
65 Gray to Binary
(101111) Gray = (?) 2
Gray 1 0 1 1 1 1
msb
Same → 0
Different → 1
Binary 11 0 1
msb
66 Gray to Binary
(101111) Gray = (?) 2
Gray 1 0 1 1 1 1
msb
Same → 0
Different → 1
Binary 11 0 1 0
msb
67 Gray to Binary
(101111) Gray = (?) 2
Gray 1 0 1 1 1 1
msb Same → 0
Different → 1
Binary 11 0 1 0 1
msb
(101111)Gray = (110101) 2
68 Decimal-Binary-Hex-BCD-Gray
Extension: In 1981, IBM introduced extended ASCII, which is an 8-bits code and
increased the character set to 256
Copyright ©2007 by Pearson Education, Inc.
Columbus, OH 43235
All rights reserved.
ASCII code
70
71 UNICODE
UNICODE is also a character encoding standard. However, it differs from ASCII
code in their scope and capabilities.
The conversion yields the exact numerical value (i.e. it involves directly
translating the decimal number into its equivalent binary representation