Data Representation: CSC 321: M&AL
Data Representation: CSC 321: M&AL
Data Representation: CSC 321: M&AL
Conversions
Rule 1
Decimal Any other Base (DIVISION Method) BUT
stop when
37 = 100101 quotient is zero
Converting from Decimal fractions to
Binary
Using the multiplication
method to convert the
decimal 0.8125 to binary,
we multiply by the radix 2.
The first product carries
into the units place.
9
Converting from Decimal fractions to
Binary
Converting 0.8125 to binary . . .
You are finished when the
product is zero, or until you
have reached the desired
number of binary places.
Our result, reading from top
to bottom is:
0.812510 = 0.11012
This method also works
with any base. Just use the
target radix as the
multiplier. 10
Brain storming?
Binary number with n-bits can represent how many
unsigned integers?
Which method will be used for
Hexadecimal to Decimal
Decimal to Hexadecimal
Decimal fraction to Hexadecimal
Hexadecimal to Binary
Direct conversion is possible
Binary to Hexadecimal
Hexadecimal Integers
Binary values are represented in hexadecimal.
Converting Binary to Hexadecimal
DIRECT COVERSION.
Example: Translate the binary integer
000101101010011110010100 to hexadecimal
M1023.swf
Converting Binary to Hexadecimal
Let
x2 n
2 + j zeros
i
( Hex of x)
Converting Binary Decimal
Bit “0”
1010112 => 1 x 20 = 1
1 x 21 = 2
0 x 22 = 0
1 x 23 = 8
0 x 24 = 0
1 x 25 = 32
4310
Converting Hexadecimal Decimal
2 31 0
15 1
2
7 1
2
2 3 1
2 1 1
0 1
12510 = 11111012
Decimal Hexadecimal
123410 = ?16
16 1234
16 77 2
16 4 13 = D
0 4
123410 = 4D216
Hexadecimal Binary
10AF16 = ?2
1 0 A F
10AF16 = 00010000101011112
Binary Hexadecimal
10101110112 = ?16
10 1011 1011
2 B B
10101110112 = 2BB16
Exercise – Convert ...
Hexa-
Decimal Binary decimal
33
1110101
1AF
Hexa-
Decimal Binary decimal
33 100001 21
117 1110101 75
451 111000011 1C3
431 110101111 1AF
Fractions
Binary to decimal
Hexa-
Decimal Binary decimal
29.8
101.1101
C.82
Don’t use a calculator!
Exercise – Convert …
Answer
Hexa-
Decimal Binary decimal
29.8 11101.110011… 1D.CC…
5.8125 101.1101 5.D
3.109375 11.000111 3.1C
12.5078125 1100.10000010 C.82
Integer Storage Sizes
byte 8
word 16
Standard sizes:
doubleword 32
quadword 64
29
Signed Magnitude
Arithmetic human-like
30
Signed Magnitude
Example:
75 + 46 = ?
31
Signed Magnitude
Example:
Using signed magnitude
binary arithmetic, find the
sum of 75 and 46.
Example:
107 +46 =?
What u get?
33
Signed Magnitude
Example:
107 +46 =?
34
Signed Magnitude
-46 - 25
35
Signed Magnitude
46-25
36
Signed Magnitude
Easy for Human
Complicated Computer Hardware
Two different representations for zero: positive zero and
negative zero. (Does K+(-K) works for both
representations?)
There is discontinuity in the number system, adding
1 to the number 7 should give 8 but it gives the
value -0. 7 + 1 should give 8 but it gives -0
0 1 1 1
1
1 0 0 0
37
Other system (Complements)
One’s complement Representation
38
One’s complement Representation
39
One’s complement Representation
40
Two's Complement representation
starting value 00100100 = +36
42
Two’s complement Representation
48 - 19
43
Overflow for Unsigned integer Addition
Unsigned overflow – result is “out of range”
Occurs if carryout of MSB is 1
Binary Check in decimal
1111 15
+ 0100 +4
(cout=1) 0011 3(?)
(unsigned overflow occurs)
44
25 represent in three ways
-25
Sm-----1’s CMP-----2’comp
Detection of overflows in (unsigned and
signed) additions and subtractions:
unsigned signed
Subtraction
46
Overflow for signed integer addition? (a)
(a) Previous example
1111 -1
+ 0100 +4
(cout=1) 0011 3(?)
(Cin =1) (NO signed overflow
occurs)
47
Overflow for signed integer addition (b)
(b) New example
0110 +6
+0110 +6
(cout=0) 1100 - 4(?)
(cin = 1) (Signed overflow occurs)
48
Overflow for signed integer addition (c)
(c) New example
1010 -6
+1001 +-7
(cout=1) 0011 3?
(cin = 0) (signed overflow occurs)
49
How do we detect overflow in signed
integer addition?
Whenever 2 positive values are added and result is
negative
Whenever 2 negative values are added and the result is
positive
What if one operand is positive and the other is negative
in addition?
Cout ≠ Cin
50
Detection of overflows in (unsigned and
signed) additions and subtractions:
unsigned signed
Subtraction
51
Subtraction in computer hardware
To compute x-y:
Perform two’s-complement operation on y
Add x and result of two’s-complement operation.
52
How does subtraction work for unsigned
integers?
The same adder for unsigned addition is used for
unsigned subtraction
A – B == A + (-B)
0101 5
- 1001 -9
0101 5
+0111 +(-9)
(cout=0) 1100 12? (unsigned overflow)
Note no carryout and the result is wrong.
53
Overflow in unsigned integer subtraction
1001 9
- 0101 -5
1001 9
+1011 +-5
(cout=1) 0100 4 (correct answer)
There is carryout and the result is correct!
Carryout of MSB == 1 means no overflow in unsigned subtraction
Carryout of MSB == 0 means overflow in unsigned subtraction
54
Overflow in signed integer subtraction
No need to implement a separate subtractor
A – B = A + (-B)
Overflow may occur
Overflow detection same as overflow addition of signed
integers
Whenever 2 positive values are added and result is negative
Whenever 2 negative values are added and the result is positive
Or Carryout of MSB Carryin of MSB
55
Detection of overflows in (unsigned and
signed) additions and subtractions:
unsigned signed
56
Overflow detection Example
Example:
107 + 46 ( in 2’s Complement)
We see that the nonzero carry
from the seventh bit overflows into
the sign bit, giving us the
erroneous result: 107 + 46 = -103.
sign bit
1 1 1 1 0 1 1 0
Negative
0 0 0 0 1 0 1 0 Positive
TMax + 1 Unsigned
TMax TMax Range
2’s Complement 0 0
Range –1
–2
TMin
Summary of integer representation