You are on page 1of 30

CS1L001 : Programming and Data Structures

Number System

Joy Mukherjee

School of Electrical Sciences


Computer Science and Engineering
Indian Institute of Technology Bhubaneswar

1 / 30
Outline

1 Bits & Bytes

2 Unsigned Integer Representation

3 Signed Integer Representation

4 Summary: 4-bit Representations of Signed Integers

5 IEEE floating point standard

2 / 30
Outline

1 Bits & Bytes

2 Unsigned Integer Representation

3 Signed Integer Representation

4 Summary: 4-bit Representations of Signed Integers

5 IEEE floating point standard

3 / 30
Bits & Bytes

1 Bit: a single 0 or 1.
2 1 Byte = Consecutive 8 bits
3 1 KiloByte(KB) = 210 Bytes
4 1 MegaByte(MB) = 210 KB = 220 Bytes
5 1 GigaByte(GB) = 210 MB = 220 KB = 230 Bytes
6 1 TeraByte(TB) = 210 GB = 220 MB = 230 KB = 240 Bytes

4 / 30
Outline

1 Bits & Bytes

2 Unsigned Integer Representation

3 Signed Integer Representation

4 Summary: 4-bit Representations of Signed Integers

5 IEEE floating point standard

5 / 30
Representation of numbers in memory

Representation Digits
Binary {0, 1}
Octal {0, 1, 2, 3, 4, 5, 6, 7}
Decimal {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Hexadecimal {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}
Table: Number Representation

6 / 30
Non-negative Decimal Non-negative Binary
1 If n is equal to 0, write the remainders in the reverse sequence.
2 Divide n by 2 and remembering the intermediate remainders obtained.
3 Remember the remainder.
4 Goto step 1.
Divide by n Remainder
2 157
2 78 1
2 39 0
2 19 1
2 9 1
2 4 1
2 2 0
2 1 0
2 0 1
(157)10 = (10011101)2
Table: Decimal to Binary

7 / 30
Non-negative Binary Non-negative Decimal

Position 7 6 5 4 3 2 1 0
Binary 1 0 0 1 1 1 0 1
Decimal 27 + 0+ 0+ 24 + 23 + 22 + 0+ 20
(10011101)2 = (157)10
Table: Binary to Decimal

8 / 30
Non-negative Decimal Non-negative Octal
1 If n is equal to 0, write the remainders in the reverse sequence.
2 Divide n by 8 and remembering the intermediate remainders obtained.
3 Remember the remainder.
4 Goto step 1.

Divide by n Remainder
8 157
8 19 5
8 2 3
8 0 2
(157)10 = (235)8
(157)10 = ({10}{011}{101})2 = (235)8
Table: Decimal to Octal

9 / 30
Non-negative Octal Non-negative Decimal

Position 2 1 0
Octal 2 3 5
Decimal 2*82+ 3*81 + 5*80
(235)8 = (157)10
Table: Octal to Decimal

10 / 30
Non-negative Decimal Non-negative Hexadecimal
1 If n is equal to 0, write the remainders in the reverse sequence.
2 Divide n by 16 and remembering the intermediate remainders
obtained.
3 Remember the remainder.
4 Goto step 1.

Divide by n Remainder
16 157
16 9 13(D)
16 0 9
(157)10 = (9D)16
(157)10 = ({1001}{1101})2 = (9D)16
Table: Decimal to Hexadecimal

11 / 30
Non-negative Hexadecimal Non-negative Decimal

Position 1 0
Hexadecimal 9 D
Decimal 9*161+ D*160
(9D)16 = (157)10
Table: Hexadecimal to Decimal

12 / 30
Outline

1 Bits & Bytes

2 Unsigned Integer Representation

3 Signed Integer Representation

4 Summary: 4-bit Representations of Signed Integers

5 IEEE floating point standard

13 / 30
Representation of Signed Integers

1 Signed Magnitude Representation


2 1s Complement Representation
3 2s Complement Representation

14 / 30
Signed Magnitude Representation

Now we add provision for sign in a t-bit signed representation of n:


1 The most significant (leftmost) bit is reserved for the sign.

1 0 means positive
2 1 means negative
2 The remaining t-1 bits store the binary representation of |n|.
Example:
1 The 7-bit binary representation of 70 is (1000110)2 .
2 The 8-bit signed magnitude representation of 70 is (01000110)2 .
3 The 8-bit signed magnitude representation of -70 is (11000110)2 .

15 / 30
Signed Magnitude Representation: Notes

1 The t-bit unsigned representation can accommodate integers in the


range 0 to 2t 1.
2 The t-bit signed magnitude representation can accommodate integers
in the range (2t1 1) to +(2t1 1).
3 The 8-bit signed magnitude representation of 0 has two renderings:
+0 = 00000000 and -0=10000000.

16 / 30
1s Complement Representation

1 1s complement of a t-bit sequence (at1 at2 . . . a0 )2 is the t-bit


sequence (bt1 bt2 . . . b0 )2 , where, for each i, bi is the bit-wise
complement of ai . Here (bt1 bt2 . . . b0 )2 = 2t -1-(at1 at2 . . . a0 )2 .
2 The t-bit 1s complement representation of an integer n is a t-bit
signed representation with the following properties:
1 The most significant (leftmost) bit is reserved for the sign.
1 0 means positive
2 1 means negative
2 The remaining t-1 bits are used to stand for the absolute value |n|.
1 If n is positive, t-1 bits hold the (t-1)-bit binary representation of |n|.
2 If n is negative, t-1 bits hold the (t-1)-bit 1s complement of |n|.

Example: The 7-bit binary representation of 70 is (1000110)2 . The 7-bit


1s complement of 70 is (0111001)2 .
1 The 1s complement representation of +70 is (01000110)2 .
2 The 1s complement representation of -70 is (10111001)2 .
17 / 30
1s Complement Representation: Notes

1 The t-bit 1s complement representation can accommodate integers


in the range (2t1 1) to +(2t1 1).
2 0 has two representations: +0 = (00000000)2 and -0 = (11111111)2 .

18 / 30
2s Complement Representation

1 2s complement of a t-bit sequence (at1 at2 . . . a0 )2 is


(ct1 ct2 . . . c0 )2 = (bt1 bt2 . . . b0 )2 + 1. Here (ct1 ct2 . . . c0 )2 =
(bt1 bt2 . . . b0 )2 + 1 = 2t - (at1 at2 . . . a0 )2 .
2 The t-bit 2s complement representation of an integer n is a t-bit
signed representation with the following properties:
1 The most significant (leftmost) bit is reserved for the sign.
1 0 means positive
2 1 means negative
2 The remaining t-1 bits are used to stand for the absolute value |n|.
1 If n is positive, t-1 bits hold the (t-1)-bit binary representation of |n|.
2 If n is negative, t-1 bits hold the (t-1)-bit 2s complement of |n|.

19 / 30
2s Complement Representation: Example & Notes

Example: The 7-bit binary representation of 70 is (1000110)2 . The 7-bit


1s complement of 70 is (0111001)2 . The 7-bit 2s complement of 70 is
(0111001)2 .
1 The 2s complement representation of +70 is (01000110)2 .
2 The 1s complement representation of -70 is (10111001)2 .
3 The 2s complement representation of -70 is (10111010)2 .
Notes:
1 The t-bit 2s complement representation can accommodate integers
in the range 2t1 to +(2t1 1).
2 0 has only one representation: (0000...0)2 .
3 The 2s complement representation simplifies implementation of
arithmetic (in hardware).

20 / 30
Outline

1 Bits & Bytes

2 Unsigned Integer Representation

3 Signed Integer Representation

4 Summary: 4-bit Representations of Signed Integers

5 IEEE floating point standard

21 / 30
Summary: 4-bit Representations of Signed Integers
Decimal Signed Magnitude 1s Complement 2s Complement
+7 0111 0111 0111
+6 0110 0110 0110
+5 0101 0101 0101
+4 0100 0100 0100
+3 0011 0011 0011
+2 0010 0010 0010
+1 0001 0001 0001
0000 0000
0 or 0r 0000
1000 1111
-1 1001 1110 1111
-2 1010 1101 1110
-3 1011 1100 1101
-4 1100 1011 1100
-5 1101 1010 1011
-6 1110 1001 1010
-7 1111 1000 1001
-8 No Representation No Representation 1000

Table: Signed Integers Representation 22 / 30


Outline

1 Bits & Bytes

2 Unsigned Integer Representation

3 Signed Integer Representation

4 Summary: 4-bit Representations of Signed Integers

5 IEEE floating point standard

23 / 30
IEEE floating point standard

Now its time for representing real numbers in binary.


n = 172.93 = 1.7293 102 = 0.17293 103
1 Integral Part: Same as before.
2 Fractional part:
1 Multiply 0.x by 2
2 Print the integral part i and store fractional part into f.
3 x=f
4 Goto step 2.1 for y times.
5 if y == 0, stop.

24 / 30
IEEE floating point standard (cont.)

Divide by Integral Part Remainder Multiply by Fractional part Integral Part


2 172 2 .93
2 86 0 2 .86 1
2 43 0 2 .72 1
2 21 1 2 .44 1
2 10 1 2 .88 0
2 5 0 2 .76 1
2 2 1 2 .52 1
2 1 0 2 .04 1
2 0 1 2 .08 0
(172)10 = (10101100)2
(0.93)10 = (11101110)2

25 / 30
IEEE floating point standard (cont.)
(172.93)10
= (10101100.11101110 . . .)2
= (1.01011001110111000...)2 27
= (0.101011001110111000...)2 28
Decimal fraction 0.93 does not have a terminating binary expansion.
Solution: Approximation of the fractioal part through truncation after
y bits.
If y = 10
(1.0101100111)2 27
= (20 + 22 + 24 + 25 + 28 + 29 + 210 ) 27
= 27 + 25 + 23 + 22 + 21 + 22 + 23
= 128 + 32 + 8 + 4 + 0.5 + 0.25 + 0.125
= 172.875
This example illustrates how to store approximate representations of real
numbers using a fixed amount of bits.
26 / 30
IEEE 754 Floating-point Format

Normal Form: If we write the expansion in the normal form with only
one 1 bit (and nothing else) to the left of the binary point, then it is
sufficient to store only the fractional part (0101100111 in our
example) and the exponent of 2 (7 in the example).
This is is done by the IEEE 754 floating-point format.
This is a 32-bit representation of signed floating point numbers. The
32 bits are used as follows:
31 30 29 ... 24 23 22 21 ... 1 0
S E7 E6 ... E1 E0 M22 M21 ... M1 M0

S: sign bit, 0 means positive, and 1 means negative.


E7 E6 . . . E1 E0 (8 bits): exponent. Usual numbers lie in the range 1 to
254.
M22 M21 . . . M1 M0 (23 bits): mantissa/significand. Allowed range 0
and 223 1.
27 / 30
Normal Numbers:IEEE standard

The normal number that this 32-bit value stores is interpreted as:
(1)S (1.M22 M21 . . . M1 M0 )2 2[(E7 E6 ...E1 E0 )2 127]
Biggest positive real number:
0 11111110 1111111 11111111 11111111
(1.11111111111111111111111) 2127 =
(2 0.000000000000000000000001) 2127 2128 3.403 1038 .
Smallest positive real number:
0 00000001 0000000 00000000 00000000
1.00000000000000000000000 2126 = 2126 1.175x1038 .

28 / 30
Denormal Numbers: IEEE standard

All the exponent bits E7 E6 . . . E1 E0 must be 0.


The 32-bit value is now interpreted as the number:
(1)S (0.M22 M21 . . . M1 M0 )2 2126
Biggest positive real number:
0 00000000 1111111 11111111 11111111 =
0.11111111111111111111111 2126 =
(1 0.00000000000000000000001) 2126 = 2126 2149 .
Minimum positive real number:
0 00000000 0000000 00000000 00000001 =
2149 1.401x1045 .

29 / 30
Special Numbers

Recall that the exponent bits were not allowed to take the value 0 or 255.
This value corresponds to some special numbers.

32-bit value Interpretation


0 1111 1111 0000000 00000000 00000000 +Inf
1 1111 1111 0000000 00000000 00000000 -Inf
0 1111 1111 Any nonzero 23-bit value NaN
1 1111 1111 Any nonzero 23-bit value NaN
0 0000 0000 0000000 00000000 00000000 +0
1 0000 0000 0000000 00000000 00000000 -0

30 / 30

You might also like