Lec-03-Data Representation
Lec-03-Data Representation
Programming
CS F111
BITS Pilani
Dubai Campus
BITS Pilani
Dubai Campus
•
Unsigned Integer Bin Rep (min bits) Bin Rep (8 bit) Bin Rep (16 bit)
3 11 00000011 0000000000000011
10 1010 00001010 0000000000001010
15 1111 00001111 0000000000001111
255 11111111 11111111 1000000011111111
256 100000000 Not Possible 0000000100000000
65536 10000000000000000 Not Possible Not Possible
b3 b2 b1 b0 Decimal
0 1 1 1 +7 +(2(4-1)-1)
0 1 1 0 +6
0 0 0 1 +1
0 0 0 0 +0
1 0 0 0 -0
1 0 0 1 -1
1 1 1 0 -6
1 1 1 1 -7 -(2(4-1)-1)
BITS Pilani, Dubai Campus
Signed Integer
Representation
Signed Integer Bin Rep (min bits) Bin Rep (8 bit) Bin Rep (16 bit)
+5 0 101 00000101 0000000000000101
-5 1 101 10000101 1000000000000101
-127 1 1111111 11111111 1000000001111111
+0 00 00000000 0000000000000000
-0 10 10000000 1000000000000000
b3 b2 b1 b0 Decimal
0 1 1 1 +7 +(2(4-1)-1)
0 1 1 0 +6
0 0 0 1 +1
0 0 0 0 +0
1 1 1 1 -0
1 1 1 0 -1
1 0 0 1 -6
1 0 0 0 -7 -(2(4-1)-1)
b3 b2 b1 b0 Decimal
0 1 1 1 +7 +(2(4-1)-1)
0 1 1 0 +6
0 0 0 1 +1
0 0 0 0 +0
1 1 1 1 -1
1 1 1 0 -1
1 0 0 1 -7
1 0 0 0 -8 -(2(4-1))
BITS Pilani, Dubai Campus
2’s Complement
Representation
• Positional number representation with a twist
• The most significant bit has a negative weight
-2(n-1) 2(n-2) 2 1 20
• e.g.:-
0110 = -1x(0x23)+ 1x22 + 1x21 + 0x20 = 4+2 = 6
1010 = -1x(1x23)+ 0x22 + 1x21 + 0x20 = -8+2 = -6
• Another method
-6 1010
(1’s Comp)0101
( Add 1) +1
6 0110
• 6 ( 4 bits) = 0110
• 6 (8 bits) = 00000110
• -1 (4 bits) = 1111
•-1 (8 bits) = 11111111
•
• -2 (4 bits) = 1110 (0010 => 1101 + 1 => 1110)
-2 (8 bits) = 11111110 ( i.e. ~00000010 => 11111101 + 1 => 11111110)
Range 0 to 7 -3 to 3 -3 to 3 -4 to 3