Professional Documents
Culture Documents
Data Representation
ITCS 222: Computer Organization and
Assembly Language
(37)10 = (100101)2
(91)10 = ( ? )2
QT RM (24)10 = ( ? )2
91/2 45 1
45/2 22 1 QT RM
22/2 11 0 24/2 12 0
11/2 5 1 12/2 6 0
5/2 2 1 6/2 3 0
2/2 1 0 3/2 1 1
1/2 0 1 1/2 0 1
7 6 5 4 3 2 1 0
1 0 0 1 1 1 0 1
27 26 25 24 23 22 21 20
Common
powers of 2
Memorize !!
11101011000101101010011110010100
Solution:
E B 1 6 A 7 9 4
1110 1011 0001 0110 1010 0111 1001 0100
Examples:
(1234)16 = (1163) + (2162) + (3 161) + (4160) = (4660)10
(3BA4)16 = (3163) + (11162) + (1016) + (4160) = (15268)10
ITCS 222– Computer Organization slide #13 Data Representation
Binary Other Bases Binary
• Computers use the binary system to represent data. In most cases a
number is represented with 16, 32, 64, or more bits, which is difficult to be
handled by humans.
• To make binary numbers easier to manipulate, we can group the bits of the
number in groups of 2, 3 or 4 bits.
• Binary ( ??)4 is performed by dividing the bin value into group of 2
bits, starting from the right side.
= (10 11 01 00)2 =
(10)2 =2 (01)2 = 1
= (2310)4
= (753)8
• Binary ( ??)16 is performed by dividing the bin value into group of 4
bits, starting from the right side.. Letters A to F are used to represent
digits from 10 to 15. (1011)2 =11= B (0100)2 = 4
(10110100)2 = ( ??? )16
(1011 0100)2=
= (B4)
ITCS 222– Computer
16 Organization slide #15 Data Representation
Solved Examples: Sum of Weights Method
3 2 1 0
(1011)2 = (1x2 )+(0x2 )+(1x2 ) +(1x2 ) = 8+0+2+1= (11)10
4 3 2 1 0
(10110)2 = (1x2 )+(0x2 )+(1x2 ) +(1x2 ) +(0x2 ) = 16+0+4+2+0= (22)10
5 4 3 2 1 0
(101100)2=(1x2 )+(0x2 )+(1x2 )+(1x2 )+(0x2 )+(0x2 )=32+0+8+4+0+0=(44)10
2 1 0
(256)8 = (2x8 )+(5x8 )+(6x8 )= 128 + 40 + 6 = (174)10
3 2 1 0
(3256)8 = (3x8 )+((2x8 )+(5x8 )+(6x8 )= 1526 + 128 + 40 + 6 = (1710)10
3 2 1 0
(4207)8 = (4x8 )+((2x8 )+(0x8 )+(7x8 )= 2048 + 128 + 0 + 7 = (2183)10
2 1 0
(2CE)16 = (2x16 )+(12x16 )+(14x16 )= 512 + 192 + 14 = (718)10
3 2 1 0
(2F5B)16 =(2x16 )+(15x16 )+(5x16 )+(11x16 )=8192+3840+80+11= (12123)10
ITCS 222– Computer Organization slide #16 Data Representation
Exercises
Perform the following number conversions:
• (53) = (?)
10 2
• (47) = (?)
10 8
• (124) = (?) 10 16
• (253) = (?) 10 16
• (97) = (?)
10 8
• (145) = (?) 10 16
• (250) = (?) 10 2
• (100) = (?) 10 8
Half Word 16
Storage Sizes
Word 32
Double Word 64
565 1 0 0 0 1 1 0 1 0 1
carry 1 1 1 1
0 0 1 1 1 1 0 1 (61)
+ 0 0 1 1 1 1 1 0 (62)
0 1 1 1 1 0 1 1 (123)
bit position: 7 6 5 4 3 2 1 0
• Example:
carry: 1 1 1
1 C 3 7 2 8 6 A
+ A + B = 10 + 11 = 21
9 3 9 5 E 8 4 B sum = 21 % 16 = 5
carry = 21 / 16 = 1
A F C D 1 0 B 5
ITCS 222– Computer Organization slide #25 Data Representation
Signed Integers
• There are three ways to represent signed numbers:
Sign-Magnitude
Biased
Complements: (1's complement, 2's complement )
• The range of signed values is divided into 2 equal parts
First part corresponds to the positive numbers (≥ 0)
Second part correspond to the negative numbers (< 0)
• In computers: Signed numbers are represented using 2's
complement representation!
Has many advantages over other representations
Used widely in processors to represent signed integers
Subtract Magnitudes
Operation ADD
Magnitudes
A >= B A<B
1 1 1 1 1 1
0 1 0 0 0 1 1 1 71 0 0 1 1 0 0 0 1 4
+ + 9
1 1 1 0 1 0 1 0 -21 0 0 0 0 0 0 0 1 +1 =
carry
0 0 1 1 0 0 0 1 49 0 0 1 1 0 0 1 0 5
0
Carry = 1 Increment Result Correct Sum = 50
unchanged
• Leave all the 0s to its right unchanged 2's Complement
• Complement all the bits to its left
= 11011 1 00
ITCS 222– Computer Organization slide #33 Data Representation
Properties of 2's Complement Representation
• Range of represented values: -2n-1 to +(2n-1 – 1)
For example, if n = 8 bits, range is -128 to +127
• There is only one zero = (0…000)2 (all bits are zeros)
1's complement of positive zero is (0…0000)2 = (0 ...0000)2
1's complement of negative zero is (1…0000)2 = (0 ...0000)2
0 0 0 0 1 0 1 0 Positive
• Examples:
2's complement of 3457 = 3457 ; not changed since it is positive
2's complement of 5724 = 2053 + 1 = 2054
2's complement of 77777777 = 00000000 + 1 = 00000001
• Examples:
2's complement of 6A3D = 6A3D ; not changed since it is positive
2's complement of 92F16AC0 = 6D0E953F + 1 = 6D0E9540
2's complement of FFFFFFFF = 00000000 + 1 = 00000001
ITCS 222– Computer Organization slide #37 Data Representation
Sign Extension
Step #1: Move the number into the lower-significant bits
Step #2: Fill all the remaining higher bits with the sign bit
• This ensures that both magnitude and sign are correct
• Examples
1) Sign-Extend 10110011 to 16 bits
1 1
(-3) 1 1 0 1 (-3) 1 1 0 1
+(+2) 0 0 1 0 +(-2) 1 1 1 0
=(-1) 1 1 1 1 =(-5) 1 0 1 1
01001101 01001101
– 00111010 + 11000110 (2's complement)
00010011 00010011 (same result)
• The biggest advantage of 2's complement is that the same
adder is used for both addition and subtraction
• Final carry is ignored, because
A + (2's complement of B) = A + (2n – B) = (A – B) + 2n
Final carry = 2n, for n-bit numbers
ITCS 222– Computer Organization slide #40 Data Representation
Binary Subtraction
• To subtract two 4-bit signed binary numbers ±A - ±B: represent both
numbers in 2's complement, align them, add the 1st value with the 2’s
complement of the 2nd value, and ignore the external carry, if any. External
carry is the carry out of the leftmost bits.
(+3) 0 0 1 1 (-3) 1 1 0 1
-(+2) 0 0 1 0 -(+2) 0 0 1 0
0 0 1 1 1 1 0 1
2’s complement 1 1 1 0 2’s complement 1 1 1 0
=(+1) 0 0 0 1 =(-5) 1 0 1 1
(+3) 0 0 1 1 (-3) 1 1 0 1
-(-2) 1 1 1 0 -(-2) 1 1 1 0
0 0 1 1 1 1 0 1
2’s complement 0 0 1 0 2’s complement 0 0 1 0
=(+5) 0 1 0 1 =(-1) 1 1 1 1
ITCS 222– Computer Organization slide #41 Data Representation
Hexadecimal Subtraction
16 + 5 = 21
Borrow: 1 1 1 Carry: 1 1 1 1 1
B14FC675 B14FC675
- +
839EA247 7C615DB9 (2's complement)
–9,223,372,036,854,775,808 to
Double Word +9,223,372,036,854,775,807
–263 to (+263 – 1)
What is the smallest and largest signed decimal can be stored in a 20-bit ?
Answer: –220-1 to +220-1 – 1
Borrow = 1 Carry = 1
Finite Set of Unsigned Integers
Subtraction Addition
Negative Positive
Finite Set of Signed Integers
Overflow Overflow
min = -2n-1 max = +2n-1–1
ITCS 222– Computer Organization
0
slide #46 Data Representation
Carry and Overflow Examples
• We can have carry without overflow and vice-versa
• Four cases are possible (Examples for adding 8-bit numbers)
1 1 1 1 1 1
0 0 0 0 1 1 1 1 15 0 0 0 0 1 1 1 1 15
+ +
0 0 0 0 1 0 0 0 8 1 1 1 1 1 0 0 0 248 (-8)
0 0 0 1 0 1 1 1 23 0 0 0 0 0 1 1 1 7
1 1 1 1
0 1 0 0 1 1 1 1 79 1 1 0 1 1 0 1 0 218 (-38)
+ +
0 1 0 0 0 0 0 0 64 1 0 0 1 1 1 0 1 157 (-99)
1 0 0 0 1 1 1 1 143 0 1 1 1 0 1 1 1 119
(-113)
Carry = 0 Overflow = 1 Carry = 1 Overflow = 1
i = n-1 j = -1
Nr = åd
i = 0
i × ri + åd
j = -m
j × rj
• Join the integer and fraction parts together with the radix point
ITCS 222– Computer Organization slide #55 Data Representation
Simplified Conversions
Converting real numbers between Binary, Octal, and
Hexadecimal can be simplified since the radix is power of 2.
Starting at the radix point, the integer part is converted
from right to left and the fractional part is converted from
left to right
Group 4 bits into a hex digit or 3 bits into an octal digit
integer: right to left fraction: left to right
7 2 6 1 3 . 2 4 7 4 5 2 Octal
111010110001011.01010011110010101 Binary
7 5 8 B . 5 3 C A 8 Hexadecimal
Use binary to convert between octal and hexadecimal
S Exponent Fraction
ITCS 222– Computer Organization slide #58 Data Representation
IEEE 754 Floating-Point Standard
• Used virtually in every computer invented since 1980
Simplifies porting of floating-point numbers among architectures
Unifies the development of floating-point algorithms
Increases the accuracy of floating-point numbers
• Single Precision Floating Point Numbers (32 bits)
1-bit sign + 8-bit exponent + 23-bit fraction
S Exponent8 Fraction23
• Double Precision Floating Point Numbers (64 bits)
1-bit sign + 11-bit exponent + 52-bit fraction
S Exponent11 Fraction52
(continued)
• ITCS 222–SComputer
= 0 forOrganization
positive numbers,
slide #62 S = 1 for negative
Data Representation
Example: Decimal to IEEE single-precision
• Convert –0.8125 to single and double-precision floating-point
• Solution:
Fraction bits can be obtained using multiplication by 2
0.8125 × 2= 1.625
0.625 × 2 = 1.25
0.25 × 2 = 0.5 0.8125 = (0.1101)2
0.5 × 2 = 1.0
Stop when fractional part is 0, or after computing all required fraction bits
Fraction = (0.1101)2 = (1.101)2 × 2 –1
(Normalized)
Exponent = –1 + Bias = 126 (single precision) and 1022 (double)
10111111111010100000000000000000
Double Precision
00000000000000000000000000000000
ITCS 222– Computer Organization slide #63 Data Representation
Example: Decimal to IEEE single-precision
Convert -23.375 to into the IEEE single-precision.
Solution:
1) Integer part is converted by repeated division by 2;
2) Fractional part is converted by repeated multiplication by 2:
23.375 = 10111.011
3) Normalize the resulting binary value:
10111.011 1.0111011000*24
4) Calculate the exponent:
Biased Exponent = 4 + 127 = 131 10000011
5) Answer in binary =
1 100 00011 011 1011 0000 0000 0000 0000
6) Answer in hexadecimal =
0xC1BB 0000
ITCS 222– Computer Organization slide #64 Data Representation
Example: IEEE single-precision to Decimal
Convert the IEEE single-precision 0xC2B56000 to into decimal.
Solution:
1) Convert the hexadecimal value into binary and split it into SEF parts
0xC2B56000 = 1 10000101 011 0101 0110 0000 0000 0000
= -1011010.10110000000000000
• Solution:
Sign = 1 is negative
E = (01111100)2 = 124; E – bias = 124 – 127 = –3
Significand=(1.0100 ... 0)2 = 1 + 2-2 = 1.25 (1. is implicit)
Value in decimal = –1.25 × 2–3 = –0.15625
• What is the decimal value of this single precision FP?
01000001001001100000000000000000
• Solution: implicit
Value in decimal = +(1.01001100 ... 0)2 × 2130–127
= (1.01001100 ... 0)2 × 23
= (1010.01100 ... 0)2 = 10.375
ITCS 222– Computer Organization slide #66 Data Representation
Examples of Double Precision Float
• Solution:
Value of exponent = (10000000101)2 – Bias = 1029 – 1023 = 6
Value of double = (1.00101010 ... 0)2 × 26 (1. is implicit)
= (1001010.10 ... 0)2 = 74.5
2(n - 1)
< M ≤ 2n
Examples:
• ASCII code for space character = 20 (hex) = 32 (decimal)
• ASCII code for 'L' = 4C (hex) = 76 (decimal)
• ASCII code for 'a' = 61 (hex) = 97 (decimal)
• ASCII code for ‘A' = 41 (hex) = 65 (decimal)
• ASCII code for “Abt 459” is 41 62 74 20 34 35 39
ITCS 222– Computer Organization slide #75 Data Representation
Control Characters
• The first 32 characters of ASCII table are used for control
• Control character codes = 00 to 1F (hexadecimal)
Not shown in previous slide
• Examples of Control Characters
Character 0 is the NULL character used to terminate a string
Character 9 is the Horizontal Tab (HT) character
Character 0A (hex) = 10 (decimal) is the Line Feed (LF)
Character 0D (hex) = 13 (decimal) is the Carriage Return (CR)
The LF and CR characters are used together
They advance the cursor to the beginning of next line
• One control character appears at end of ASCII table
Character 7F (hex) is the Delete (DEL) character