Sie sind auf Seite 1von 28

EC381

Lecture 2

Fall 2017

Mohamed Elgalhud
Number Systems Representation - General
Form
 A number with radix r is represented by a string of
digits:
An - 1An - 2 … A1A0 . A- 1 A- 2 … A- m + 1 A- m
in which 0 Ai < r and . is the radix point.
 The string of digits represents the power series:

Number = ( i=n-1
Ai r )+(
i
j=-1

Aj r) j

i=0 j=-m
(Integer Portion) + (Fraction Portion)

2 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


General Decimal Binary
Radix (Base) r 10 2
Digits 0 => r - 1 0 => 9 0 => 1
0 r0 1 1
1 r1 10 2
2 r2 100 4
3 r3 1000 8
Powers of 4 r4 10,000 16
Radix 5 r5 100,000 32
-1 r -1 0.1 0.5
-2 r -2 0.01 0.25
-3 r -3 0.001 0.125
-4 r -4 0.0001 0.0625
-5 r -5 0.00001 0.03125

3 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


Octal and Hexadecimal
 Strings of 0s and 1s too hard to write
 Use base-16 or hexadecimal – 4 bits
 Use base-8 – 3 bits
Dec Bin Hex Oct Dec Bin Hex Oct
0 0000 0 0 8 1000 8 10
1 0001 1 1 9 1001 9 11
2 0010 2 2 10 1010 A 12
3 0011 3 3 11 1011 B 13
4 0100 4 4 12 1100 C 14
5 0101 5 5 13 1101 D 15
6 0110 6 6 14 1110 E 16
7 0111 7 7 15 1111 F 17

4 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


Number Systems – Representation
 Examples
 (7392)10=

 (11010.11)2 = (?) 10

 (127.4)8= (?) 10

 (B65F)16 =(?) 10

5 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


Examples
 Examples
 (7392)10=7 × 103+3 × 102+9× 101+2× 100
 (11010.11)2 = (?) 10
 1*24 +1*23 +0*22 +1*21 +0*20 +1*2-1 +1*2-2 =26.75
 (127.4)8= (?) 10
 (127.4)8 =1*82 +2*81 +7*80 +4*8-1 =(87.5)10
 (B65F)16 =(?) 10
 (B65F)16 =11*163 +6*162 +5*161 +15*160 =(46,687)10

6 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


Examples of Numbers with Fractions
 (2409.87)10 = 2×103 + 4×102 + 9 + 8×10-1 + 7×10-2

 (1101.1001)2 = 23 + 22 + 20 + 2-1 + 2-4 = 13.5625

 (703.64)8 = 7×82 + 3 + 6×8-1 + 4×8-2 = 451.8125

 (A1F.8)16 = 10×162 + 16 + 15 + 8×16-1 = 2591.5

 (423.1)5 = 4×52 + 2×5 + 3 + 5-1 = 113.2

 (263.5)6 Digit 6 is NOT allowed in radix 6

Reference: Lecture Notes of Dr. Samir Elbuni


Dec Hex
0 0
Hex to Decimal Example 1 1
2 2
3 3
 Just multiply each hex digit by decimal 4 4
value, and add the results. 5 5
6 6
0x2ac 7 7
8 8

2 • 256 + 10 • 16 + 12 • 1 = 684 9
10
9
a
11 b
    12 c
163 162 161 160 13 d
4096 256 16 1 14 e

8 9/26/2017
15 f
Reference: Lecture Notes of Eng.Yusra Maatog
Conversion from Decimal to other bases
 To Convert the Integral Part:
Repeatedly divide the number by the new radix and save the
remainders. The digits for the new radix are the remainders in
reverse order of their computation. Keep dividing until the quotient
becomes zero. If the new radix is > 10 (r=16), then convert all
remainders > 10 to digits A, B, …

 To Convert the Fractional Part:


Repeatedly multiply the fraction by the new radix and save the
integer digits that result. The digits for the new radix are the
integer digits in order of their computation. If the new radix is >
10, then convert all integers > 10 to digits A, B, …

9 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


Example: Convert 46.687510 To Binary
 Convert 46 to Base 2

 Convert 0.6875 to Base 2:

 Join the results together with the radix


point:

10 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


Example: Convert 46.687510 To Binary

46/2 = 23 rem = 0 LSD


23/2 = 11 + 1 / 2 rem = 1
11/2 = 5 + 1 / 2 rem = 1
5/2 = 2 + 1 / 2 rem = 1
2/2 = 1 rem = 0
1/2 = 0 + 1 /2 rem = 1 MSD
46  101110

11 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


Example: Convert 46.687510 To Base 2

0.6875 x 2 = 1.3750 integer= 1 MSD


0.3750 x 2 = 0.7500 =0
0.7500 x 2 = 1.5000 =1
0.5000 x 2 = 1.0000 =1

(0.6875)10 = (0.1011)2

12 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


Checking the Conversion
From the prior conversion of 46.687510
1011102 = 1·32 + 0·16 +1·8 +1·4 + 1·2 +0·1
= 32 + 8 + 4 + 2
= 46
0.10112 = 1/2 + 0 + 1/8 + 1/16
= 0.5000 + 0.1250 + 0.0625
= 0.6875

13 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


 Convert (153)10 to octal
 r=8  divide 153 by 8

14 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


 Convert (0.513)10 to octal.

15 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


 Convert (0.513)10 to octal.
0.513 * 8 = 4.104
0.104 * 8 = 0.832
0.832 * 8 = 6.656
0.656 * 8 = 5.248
0.248 * 8 = 1.984
0.984 * 8 = 7.872
 The answer is obtained from the integer part of the
products:
 (0.513)10 = (0.406517….)8

16 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


Dec Hex
0 0
Decimal to Hex 1 1
2 2
Convert 684 to Hex 3 3
4 4
684/16 = 42 rem 12=c 0x__c 5 5
6 6
7 7
42/16 = 2 rem 10=a 0x_ac 8 8
9 9
10 a
2/16 = 0 rem=2 0x2ac 11 b
12 c
   
13 d
163 162 161 160 14 e
4096
17
256 16 1 9/26/2017
15 f
Reference: Lecture Notes of Eng.Yusra Maatog
Algorithm – Decimal to Binary(Faster)

1. Find largest power-of-two smaller than


decimal number
2. Make the appropriate binary digit a ‘1’
3. Subtract the power of 2 from decimal
4. Do the same thing again

18 Reference: Lecture Notes of Eng.Yusra Maatog 9/26/2017


Decimal  Binary Example
 Convert 28 decimal to binary
32 is too large, so use 16
Binary  10000 Decimal  28 – 16 = 12
Next is 8
Binary  11000 Decimal  12 – 8 = 4
Next is 4
Binary  11100 Decimal  4 – 4 = 0
7 6 5 4 3 2 1 0
27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

19 9/26/2017
Reference: Lecture Notes of Eng.Yusra Maatog
Conversion Between Bases
 To convert from one base to another:
1) Convert the Integer Part
2) Convert the Fraction Part
3) Join the two results with a radix point

20 9/26/2017
Reference: Lecture Notes of Eng.Yusra Maatog
Octal to Binary
Restate the octal as three binary digits starting at the
radix point and going both ways.
Example:

21 9/26/2017
Reference: Lecture Notes of Eng.Yusra Maatog
Binary to Octal
 Group the binary digits into three bit groups starting at
the radix point and going both ways.
 Convert each group of three bits to an octal digit.
Example
 (100111100101)2 = (4745)8

22 9/26/2017
Reference: Lecture Notes of Eng.Yusra Maatog
Binary to Octal
 Group the binary digits into three bit groups starting at
the radix point and going both ways.
 Convert each group of three bits to an octal digit.
Example
 (100111100101)2 = (4745)8

23 9/26/2017
Reference: Lecture Notes of Eng.Yusra Maatog
Octal (Hex) to Binary and Back
 Hexadecimal to Binary:
 Restate the hexadecimal as four binary digits starting at the radix
point and going both ways.

 Binary to Hexadecimal
 Group the binary digits into four bit groups starting at the radix
point and going both ways
 Convert each group of four bits to an hexadecimal digit.
 (100111100101)2 = (9E5)16

 (10110001101011.11110010)2 =( ?)16

24 9/26/2017
Reference: Lecture Notes of Eng.Yusra Maatog
Bin Hex
0000 0
Hex to Binary 0001 1
0010 2
 Hex to Binary – just convert digits
0011 3
0100 4
0101 5

0x2ac 0110
0111
6
7
1000 8
1001 9
0010 1010 1100 1010 a
1011 b
0x2ac = 001010101100
1100 c
1101 d
1110 e
1111 f

25 9/26/2017
Reference: Lecture Notes of Eng.Yusra Maatog
Bin Hex
0000 0
Binary to Hex
0001 1
0010 2
 Just convert groups of 4 bits 0011 3
0100 4

101001101111011 0101 5
0110 6

0101  0011  0111  1011 0111


1000
7
8
1001 9
1010 a
5 3 7 b 1011 b
1100 c
1101 d
101001101111011 = 0x537b 1110 e
1111 f

26 9/26/2017
Reference: Lecture Notes of Eng.Yusra Maatog
Octal to Hexadecimal via Binary
 Convert octal to binary.
 Use groups of four bits and convert as the previous example
to hexadecimal digits.
 Example: Octal to Binary to Hexadecimal
(6 3 5 . 1 7)8
110 011 101 001 111

110011101.001111
(0001)(1001)(1101).(0011)(1100)
1 9 D . 3 C  (635.17) = 0x19D.3C
 The same way can be used to convert Hexadecimal to Octal
i.e. Hexadecimal  Binary  Octal

27 9/26/2017
Reference: Lecture Notes of Eng.Yusra Maatog
Summary
 Conversion between Binary/Octal/Hex
1. Nice simple ways to convert between these three number
systems, since all are a power of 2

2. Binary to Octal simply requires grouping bits into groups of


3-bits and converting

3. Binary to Hex simply requires grouping bits into groups of 4-


bits and converting

4. Going the other direction (Octal to Binary or Hex to Binary)


should follow…

28 9/26/2017
Reference: Lecture Notes of Eng.Yusra Maatog

Das könnte Ihnen auch gefallen