11 Number Systems
11 Number Systems
1
Number Representation
BINARY
HEXADECIMAL
DECIMAL
2
Topics to be Discussed
How much space (memory locations) is allocated for each type of data?
• int, float, char, double, etc.
3
Number System: The Basics
Example:
4
Binary Number System
Two digits:
• 0 and 1.
• Every digit position has a weight which is a power of 2.
• Base or radix is 2.
Example:
110 = 1 x 22 + 1 x 21 + 0 x 20
5
Binary-to-Decimal Conversion
Each digit position of a binary number has a weight.
• Some power of 2.
A binary number:
B = bn–1 bn–2 … b1 b0 . b–1 b–2 … b–m
6
Examples
1. 101011 ⇒ 1 x 25 + 0 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 = 43
(101011)2 = (43)10
7
Decimal-to-Binary Conversion
Consider the integer and fractional parts separately.
For the integer part,
• Repeatedly divide the given number by 2, and go on accumulating the remainders, until
the number becomes zero.
• Arrange the remainders in reverse order.
For the fractional part,
• Repeatedly multiply the given fraction by 2.
• Accumulate the integer part (0 or 1).
• If the integer part is 1, chop it off.
• Arrange the integer parts in the order they are obtained.
8
Example 1 :: 239
2 239
2 119 --- 1
2 59 --- 1
2 29 --- 1
2 14 --- 1
(239)10 = (11101111)2
2 7 --- 0
2 3 --- 1
2 1 --- 1
2 0 --- 1
9
Example 2 :: 64
2 64
2 32 --- 0
2 16 --- 0
2 8 --- 0
2 4 --- 0
(64)10 = (1000000)2
2 2 --- 0
2 1 --- 0
2 0 --- 1
10
Example 3 :: .634
.634 x 2 = 1.268
.268 x 2 = 0.536
.536 x 2 = 1.072
(.634)10 = (.10100…)2
.072 x 2 = 0.144
.144 x 2 = 0.288
:
:
11
Example 4 :: 37.0625
(37)10 = (100101)2
(.0625)10 = (.0001)2
12
Hexadecimal Number System
0 ⇒ 0000 8 ⇒ 1000
1 ⇒ 0001 9 ⇒ 1001
2 ⇒ 0010 A ⇒ 1010
3 ⇒ 0011 B ⇒ 1011
4 ⇒ 0100 C ⇒ 1100
5 ⇒ 0101 D ⇒ 1101
6 ⇒ 0110 E ⇒ 1110
7 ⇒ 0111 F ⇒ 1111
13
Binary-to-Hexadecimal Conversion
For the integer part,
• Scan the binary number from right to left.
• Translate each group of four bits into the corresponding hexadecimal digit.
• Add leading zeros if necessary.
14
Examples
1. (1011 0100 0011)2 = (B43)16
15
Hexadecimal-to-Binary Conversion
Translate every hexadecimal digit into its 4-bit binary equivalent.
• Discard leading and trailing zeros if desired.
Examples:
(3A5)16 = (0011 1010 0101)2
(12.3D)16 = (0001 0010 . 0011 1101)2
(1.8)16 = (0001 . 1000)2
16
Representation of
Unsigned and Signed Integers
17
Unsigned Binary Numbers
An n-bit binary number
B = bn–1bn–2 … b2b1b0
• 2n distinct combinations are possible, 0 to 2n−1.
18
Signed Integer Representation
Many of the numerical data items that are used in a program are signed (positive or negative).
• Question:: How to represent sign?
19
Sign-magnitude Representation
For an n-bit number representation
• The most significant bit (MSB) indicates sign
0 ⇒ positive
1 ⇒ negative
• The remaining n-1 bits represent magnitude.
bn–1 bn-2 b1 b0
Sign
Magnitude
20
Contd.
Range of numbers that can be represented:
Maximum :: + (2n-1 – 1)
Minimum :: − (2n-1 – 1)
A problem:
Two different representations of zero.
+0 ⇒ 0 000…0
−0 ⇒ 1 000…0
21
One’s Complement Representation
Basic idea:
• Positive numbers are represented exactly as in sign-magnitude form.
• Negative numbers are represented in 1’s complement form.
22
Example :: n = 4
0000 ⇒ +0 1000 ⇒ -7
0001 ⇒ +1 1001 ⇒ -6
0010 ⇒ +2 1010 ⇒ -5
0011 ⇒ +3 1011 ⇒ -4
0100 ⇒ +4 1100 ⇒ -3
0101 ⇒ +5 1101 ⇒ -2
0110 ⇒ +6 1110 ⇒ -1
0111 ⇒ +7 1111 ⇒ -0
A problem:
Two different representations of zero.
+0 ⇒ 0 000…0
−0 ⇒ 1 111…1
Basic idea:
• Positive numbers are represented exactly as in sign-magnitude form.
• Negative numbers are represented in 2’s complement form.
25
Example :: n = 4
0000 ⇒ +0 1000 ⇒ -8
0001 ⇒ +1 1001 ⇒ -7
0010 ⇒ +2 1010 ⇒ -6
0011 ⇒ +3 1011 ⇒ -5
0100 ⇒ +4 1100 ⇒ -4
0101 ⇒ +5 1101 ⇒ -3
0110 ⇒ +6 1110 ⇒ -2
0111 ⇒ +7 1111 ⇒ -1
Advantage:
• Unique representation of zero.
• Subtraction can be done using addition.
• Leads to substantial saving in circuitry.
Almost all computers today use the 2’s complement representation for storing negative numbers.
27
Contd.
In C, typically:
• char
• 8 bits ⇒ + (27– 1) to –27
• short int
• 16 bits ⇒ + (215−1) to −215
• int
• 32 bits ⇒ + (231−1) to −231
• long int
• 64 bits ⇒ + (263−1) to −263
28
Binary operations
29
Binary addition
Rules for adding two bits Rules for adding three bits
0+0 is 0
0+1 is 1 a b cin cout s
1+0 is 1 _____________________
1+1 is 10, that is, 0 with carry of 1 0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
_____________________
30
Subtraction Using Addition :: 1’s Complement
How to compute A – B ?
• Compute the 1’s complement of B (say, B1).
• Compute R = A + B1
• If the carry obtained after addition is ‘1’
• Add the carry back to R (called end-around carry).
• That is, R = R + 1.
• The result is a positive number.
Else
• The result is negative, and is in 1’s complement form.
31
Example 1 :: 6 – 2
1’s complement of 2 = 1101
6 :: 0110
–2 :: 1101 A
Assume 4-bit representations.
1 0011 B1 Since there is a carry, it is added back to the
1 R result.
0100 ⇒ +4 The result is positive.
End-around
carry
32
Example 2 :: 3 – 5
1’s complement of 5 = 1010
3 :: 0011
–5 :: 1010 A
1101 B1 Assume 4-bit representations.
R Since there is no carry, the result is negative.
1101 is the 1’s complement of 0010, that is, it
–2 represents –2.
33
Subtraction Using Addition :: 2’s Complement
How to compute A – B ?
• Compute the 2’s complement of B (say, B2).
• Compute R = A + B2
• If the carry obtained after addition is ‘1’
• Ignore the carry.
• The result is a positive number.
Else
• The result is negative, and is in 2’s complement form.
34
Example 1 :: 6 – 2
2’s complement of 2 = 1101 + 1 = 1110
6 :: 0110
–2 :: 1110 A
1 0100 B2 Assume 4-bit representations.
Presence of carry indicates that the result is
R positive.
+4
No need to add the end-around carry like in 1’s
complement.
Ignore carry
35
Example 2 :: 3 – 5
2’s complement of 5 = 1010 + 1 = 1011
3 :: 0011
–5 :: 1011 A
1110 B2
R
-2 Assume 4-bit representations.
Since there is no carry, the result is negative.
1110 is the 2’s complement of 0010, that is, it
represents –2.
36
2’s complement arithmetic: More Examples
▪ Example 1: 18 – 11 = ?
▪ 18 is represented as 00010010
▪ 11 is represented as 00001011
• 1’s complement of 11 is 11110100
• 2’s complement of 11 is 11110101
▪ Add 18 to 2’s complement of 11
00010010
+ 11110101
---------------- 00000111 is 7
00000111 (with a carry of 1
which is ignored)
37
2’s complement arithmetic: More Examples
▪ Example 2: 7 – 9 = ?
▪ 7 is represented as 00000111
▪ 9 is represented as 00001001
• 1’s complement of 9 is 11110110
• 2’s complement of 9 is 11110111
▪ Add 7 to 2’s complement of 9
00000111
+ 11110111 11111110 is –2
----------------
11111110 (with a carry of 0
which is ignored) 38
Overflow and Underflow
Adding two +ve (-ve) numbers should not produce a –ve (+ve) number.
If it does, overflow (underflow) occurs
40
Fixed Point Representation
41
Limitations of using Fixed Point Representation
Floating point numbers address this issue, and is made of fixed point
signed-magnitude number and an accompanying scale factor. 42
Normalization
Write a positive non-zero number as
Examples
+1010001.1101 ←6 + 1.0100011101 x 26
–111.000011 ←2 – 1.11000011 x 22
Here, s is the sign bit, f is the mantissa (fractional part), and E is the exponent (plus 127).
The 1 before the binary point is not stored.
44
IEEE standards for floating-point representation
45
Example
Solution
0 10000101 01000111001000000000000
46
Example of floating-point representation
47
Example
1 01111100 11001100000000000000000
Solution
48
Range of normalized numbers
normalized normalized
negative numbers underflow positive numbers
overflow overflow
49
Denormalized numbers
● These numbers correspond to the 8-bit exponent E = 0
● For each decrement of M by 1, the value of the denormalized number reduces by 2–149. The smallest
positive denormalized number is 2–149 (corresponding to M = 00000000000000000000001).
● When all bits of M are zero, we get the representation of +0 as a string of 32 zero bits.
51
A program to view the floating-point representation
#include <stdio.h>
int main ()
void prn32 ( unsigned a ) {
{ float x = -123.45;
int i; unsigned *p;
Output
1 10000101 11101101110011001100110
52
Check for correctness
● 123 = 64 + 32 + 16 + 8 + 2 + 1 = 26 + 25 + 24 + 23 + 21 + 20 = 1111011
● 0.45 x 2 = 0.90, 0.90 x 2 = 1.80, 0.80 x 2 = 1.60, 0.60 x 2 = 1.20, 0.20 x 2 = 0.40, 0.40 x 2 = 0.80, …
● 0.45 = 0.0111001100