Professional Documents
Culture Documents
Lec3-Intro To Number System
Lec3-Intro To Number System
Programming
Saqib Rizwan
2 13
2 6 ---- 1
2 3 ---- 0
1 ---- 1
(1101)2
Converting decimal to binary
Example: 162:
162 / 2 = 81 rem 0
81 / 2 = 40 rem 1
40 / 2 = 20 rem 0
20 / 2 = 10 rem 0
10 / 2 = 5 rem 0
5/2 = 2 rem 1
2/2 = 1 rem 0
1/2 = 0 rem 1
So, 16210 = 101000102
Some Exercises
13D = (?) B
1101B
23D = (?) B
10111B
72D = (?)
1001000B
B
Some Exercises
59D = (?) B
111011B
145 D = (?) B
10010001 B
Conversion from binary number
system to decimal system
Hexadecimal Number System
Base is 16 or ‘Hex’ or ‘H’
16 symbols: {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
Each place is weighted by the power of 16
Example:
(3AB)H
Conversion from Decimal to
Hexadecimal
Same as that of Decimal to Binary
Repeatedly divide by 16
16 93
5 ---- D
Conversion from Hexadecimal to
Decimal
(3AB4)H to Base 10
Binary to Hexadecimal
Combine every 4 bits into one hexadecimal
digit
Octal Number System
Base is 8
8 symbols: {0,1,2,3,4,5,6,7}
Each place is weighted by the power of 8
Example:
(345)8
Conversion from Decimal to Octal
Repeatedly divide by 8
then to hexadecimal:
0010 0010 1111
2 2 F
Therefore, 10578 = 22F16.
Octal to Binary
Expand each octal digit to 3 binary bits.
Example: (725)8 = (111 | 010 | 101)2
Binary to Octal
Combine every 3 bits into one octal digit
Example: (110 | 010 | 011)2 = (623)8
Some important Conversions to
Remember
Base 10 Base 2 Base 16
0 0 0
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
Unsigned Binary (UB)
Rep.
With word size of k bits, we can represent
exactly 2k integers, ranging from 0 to 2k - 1
Example (for k = 2):
UB number Dec. number
00 0
01 1
10 2
11 3
19
Signed number
representation
In ordinary arithmetic, a negative number is
indicated by a minus sign and a positive
number by a plus sign
In computers, the convention is to represent
the sign with an extra bit placed in the
leftmost position (the sign bit) and make the
sign bit 0 for positive and 1 for negative
We’ll look at three different representations
for negative numbers
20
Signed-Magnitude (SM) Rep
Idea: The leftmost bit represents the sign (0
= plus, 1 = minus) and the rest of the bits
represent the number
Example:
Sign bit = 0 = +
5 magnitude bits
(+22)10 = (0 1 0 1 1 0)SM
(-22)10 = (1 1 0 1 1 0)SM
21
SM Numbers (for k = 3)
SM number Dec. number
0000 Number Range:
0 0 11 - (2k-1 - 1) to
0 1 02
+ (2k-1 - 1)
0 1 13
1 0 0 -0
1 0 1 -1
1 1 0 -2
1 1 1 -3
22
1’s complement (1C)
representation
Like SM, 1C representation also has a sign
bit with 0 for plus and 1 for minus
Positive-number representations same as
with SM but negative-number
representations different from SM
To change the sign of a number, all the bits
in the number (not just the sign bit) are
“flipped”
23
1C Example
6-bit 1C representation of decimal 22:
Sign bit = 0 = +
5 magnitude bits = 22
(+22)10 = (0 1 0 1 1 0)1C
6-bit 1C representation of decimal -22:
(-22)10 = - (0 1 0 1 1 0)1C
= (1 0 1 0 0 1)1C
Sign bit = 1 = minus
1’s complement of (0 1 0 1 1 0)
24
1C Numbers (for k = 3)
1C number Dec. number
0000 Number Range:
0 0 11 - (2k-1 - 1) to
0 1 02
+ (2k-1 - 1)
0 1 13
1 0 0 -3
1 0 1 -2
1 1 0 -1
1 1 1 -0
25
2’s complement (2C)
representation
Like SM, 2’s complement also has a sign bit
with 0 used for plus and 1 for minus
Negative-number representations different
from SM and 1C
To change the sign of a number, all the bits
in the number are flipped (1’s
complement), then a 1 is added to the
result
26
2C Example
6-bit 1C representation of decimal 22:
Sign bit = 0 = +
5 magnitude bits
(+22)10 = (0 1 0 1 1 0)2C
(-22)10 = 101001 2’s complement of
+ 1 of (0 1 0 1 1 0)
= (1 0 1 0 1 0)2C
27
2C Numbers (for k = 3)
2C number Dec. number
0000 Number Range:
0 0 11 - 2k-1 to
0 1 02
+ (2k-1 - 1)
0 1 13
1 0 0 -4
1 0 1 -3
1 1 0 -2
1 1 1 -1
28
Single bit Binary Addition
Addition table for binary digits:
0 0 1 1
+0 +1 +0 +1
00 01 01 10
sum bit
carry bit
29
Multi-bit binary addition
When adding binary numbers if a carry is
generated, it is carried one position to the
left, just as in decimal arithmetic
Example 1 1 1
X 173 1 0 1 0 1 1 0 1
+Y 44 0 0 1 0 1 1 0 0
217 1 1 0 1 1 0 0 1
30
Addition in 1C and 2C
Suppose we want to add decimal 3 and decimal -2
using 3-bit 1C and 2C
Decimal 1’s complement 2’s complement
3 0 1 1 0 1 1
+ (-2) +1 0 1 + 1 1 0
+ 1 carry 1 0 0 0 10 0 1
1
31
Number Rep. Revisited (k
= 3)
Think of the differences
Bit pattern UB SM 1C 2C
000 0 0 0 0
0 0 1 1 1 1 1
0 1 0 2 2 2 2
0 1 1 3 3 3 3
1 0 0 4 -0 -3 -4
1 0 1 5 -1 -2 -3
1 1 0 6 -2 -1 -2
1 1 1 7 -3 -0 -1
32
Example for Two’s Complement
Notation
Two’s complement notation
Consider a machine with 32-bit integers.
Suppose int value = 13;
The 32-bit representation of value is
00000000 00000000 00000000 00001101
To form the negative of value we first form its
one’s complement by applying C bitwise
complement operator (~)
onesComplementOfValue = ~value;
33
Two’s Complement Notation
34
Two’s Complement Notation
35
Computer’s Addition
x = a – value;
x = a + (~value + 1);
Suppose a = 27 and value is 13:
00000000 00000000 00000000 00011011
11111111 11111111 11111111 11110011
00000000 00000000 00000000 00001110
Which is indeed equal to 14.
36
Floating-Point Notation
In contrast to the storage of integers,
the storage of a value with a fractional
part requires that we store not only the
pattern of 0s and 1s representing its
binary representation but also the
position of the radix point ‘.’.
How are fractions represented in binary?
A method based on scientific notation is
called floating-point notation.
37
Representation for Floating-Point
Floating-point representation
mb e
exponent
(integer)
mantissa
(fraction) base
(10 or 8 or
e.g. 156.78 is 0.15678x103 2) e.g. 0.0294 is 0.294x10-1
38
Binary Data Representation
39
Decimal to Binary Mantissa
To convert decimal fractions to binary fraction, continually
multiply fraction by base (2). The integer parts of the results
give the digits from right to left.
0.6875
0.10112
2
1.375
2
0.750
2
1.500
2
1.000
40
Example
12.3910
Separate the integer and fraction part
Convert the integer part 0.39
1210 = 11002 * 2
Convert the fractional part 0.78
* 2
0.3910 = 01102 1.56
* 2
12.3910 = 1100.01102 1.12
* 2
41
0.24
Number Representation (16-bit
example)
Sign of Note: limited number of
number digits to represent
mantissa
00 1 1 11 1 1 0 0 0 0 0 0 0 0 0 0
exponent mantissa
(with sign)
Sign (31)
43
IEEE 754 single precision format (32-bit format)
for Floating Point numbers
44
Example: 8.125
Exponent(23-30) Fraction (0:22)
Sign (31)
Result: 01000001000000100000000000000000
45
Example: −16.25
Exponent(23-30) Fraction (0:22)
Sign (31)
Result: 11000001100000100000000000000000
46
Floating Point Representations
• If stored exponent is not zero, mantissa is
interpreted as having a leading 1
1 10000111 01000000000000000000000
• This is a negative number
• Because sign is 1
– Exponent ??
• 135-127=8
– Mantissa is = .012 = 0.2510
– Becomes = 1.012 = 1.2510
– Value is: -1.25 * 28 = -32010
47