Professional Documents
Culture Documents
Base Conversions
Integer Storage Sizes Binary and Hexadecimal Addition Signed Integers and 2's Complement Notation Sign Extension Binary and Hexadecimal subtraction Carry and Overflow Character Storage
Data Representation slide 1
XXVII Roman numerals (not positional) 27 Radix-10 or decimal number (positional) 110112 Radix-2 or binary number (also positional)
Fixed-radix positional representation with k digits Number N in radix r = (dk1dk2 . . . d1d0)r Value = dk1r k1 + dk2r k2 + + d1r + d0
Examples:
Binary Numbers
Each binary digit (called bit) is either 1 or 0 Bits have no inherent meaning, can represent
Unsigned and signed integers Characters Floating-point numbers Images, sound, etc.
Most Significant Bit
7 6 5 4 3
1
27
0
26
0
25
1
24
1
23
1
22
0
21
1
20
Bit Numbering
Least significant bit (LSB) is rightmost (bit 0) Most significant bit (MSB) is leftmost (bit 7 in an 8-bit number)
Data Representation slide 3
1
27
0
26
0
25
1
24
1
23
1
22
0
21
1
20
Data Representation
slide 4
37 = (100101)2
Hexadecimal Integers
16 Hexadecimal Digits: 0 9, A F
More convenient to use than binary numbers
Data Representation
slide 6
Example:
Convert the 32-bit binary number to hexadecimal 1110 1011 0001 0110 1010 0111 1001 0100 Solution:
E 1110 B 1 6 A 7 9 4 0100
Data Representation
slide 7
Examples:
(1234)16 = (1 163) + (2 162) + (3 16) + 4 =
Storage Sizes
Word
Double What Word is the
0 to 4,294,967,295
0 to (232 1)
64
1)
Binary Addition
Start with the least significant bit (rightmost bit) Add each pair of bits Include the carry in the addition, if present
carry 1 1 1 1
0
0
1
0
1
1
0
1
1
1
1
0
0
1
(54)
(29)
(83)
0
bit position: 7
1
6
0
5
1
4
0
3
0
2
1
1
1
0
Data Representation
slide 11
Hexadecimal Addition
Start with the least significant hexadecimal digits Let Sum = summation of two hex digits If Sum is greater than or equal to 16
Sum = Sum 16 and Carry = 1
Example:
carry:
1 1 1 A + B = 10 + 11 = 21 Since 21 16 Sum = 21 16 = 5 Carry = 1
slide 12
Signed Integers
Several ways to represent a signed number
Sign-Magnitude Biased 1's complement 2's complement
Signed Integer
3 major representations:
4-bit machine word 16 different values can be represented Roughly half are positive, half are negative
Data Representation
slide 14
+0
0000 0001 0010
+1 +2 +3 +4 +5
+ 0 100 = + 4 1 100 = - 4 -
-1
+6
Data Representation
+7 -0 High order bit is sign: 0 = positive (or zero), 1 = negative Three low order bits is the magnitude: 0 (000) thru 7 (111) Number range for n bits = +/-2n-1 -1 Two representations for 0 slide 15
+0
0000 0001 0010
+1 +2 +3 +4 +5
+ 0 100 = + 4 1 011 = - 4 -
-6
+6
-7
+7
Data Representation
Subtraction implemented by addition & 1's complement Still two representations of 0! This causes some problems slide 16 Some complexities in addition
-7 -6 -5 -4 -3 -2
1100 1011 1010 1001 1110 1101 1111
+0
0000 0001 0010
+1 +2 +3 +4 +5
+ 0 100 = + 4 1 100 = - 4 -
-1
+6
-0
+7
High order bit is sign: 0 = positive (or zero), 1 = negative Three low order bits is the magnitude: 0 (000) thru 7 (111) Number range for n bits = +/-2n-1 -1 Two representations for 0
Data Representation slide 17
+0
0000 0001 0010
+1 +2 +3 +4 +5
+ 0 100 = + 4 1 011 = - 4 -
-6
+6
-7
+7
Data Representation
Subtraction implemented by addition & 1's complement Still two representations of 0! This causes some problems Some complexities in addition
slide 18
-1 -2 -3
like 1's comp except shifted one position clockwise
1110 1101 1111
+0
0000 0001 0010
+1 +2 +3 +4 +5
+ 0 100 = + 4 1 100 = - 4 -
-4 -5 -6
-7
+6
-8
Data Representation
+7
Only one representation for 0 One more negative number than positive number
slide 19
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
Data Representation
1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1
1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1
1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1
Signed value 0
+1 +2 ... +126 +127 -128 -127 ... -2
slide 21
Negative numbers
Signed value = Unsigned value 2n n = number of bits
0
1 2 ... 126 127 128 129 ... 254 255
0
8
1
4
0
2
0
1
-128 64
= -128 + 32 + 16 + 4 = -76
Data Representation
11111111
-1
11011100 = -36
Binary Value
= 00100 1 00
least significant 1
Sign Bit
Highest bit indicates the sign
1 = negative 0 = positive
Sign bit
1 1 1 1 0 1 1 0
Negative
Positive
For Hexadecimal Numbers, check most significant digit If highest digit is > 7, then value is negative Examples: 8A and C5 are negative bytes B1C42A00 is a negative word (32-bit signed integer)
Data Representation slide 23
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 will ensure that both magnitude and sign are correct
Examples
Sign-Extend 10110011 to 16 bits 10110011 = -77 01100010 = +98 11111111 10110011 = -77 00000000 01100010 = +98 Sign-Extend 01100010 to 16 bits
Examples:
2's complement of 6A3D = 95C2 + 1 = 95C3 2's complement of 92F15AC0 = 6D0EA53F + 1 = 6D0EA540 2's complement of FFFFFFFF = 00000000 + 1 = 00000001
Data Representation
slide 25
Binary Subtraction
When subtracting A B, convert B to its 2's complement
Add A to (B)
borrow: 1 1 1
carry: 1 1
1 1
01001101 00111010
00010011
01001101 + 11000110
00010011
Hexadecimal Subtraction
16 + 5 = 21
Borrow: 1 1 1 Carry: 1 1 1 1 1
When a borrow is required from the digit to the left, then Add 16 (decimal) to the current digit's value Last Carry is ignored
Data Representation slide 27
9,223,372,036,854,775,808 to Double Word 263 to (263 1) +9,223,372,036,854,775,807 Practice: What is the range of signed values that may be stored in 20 bits?
Data Representation
slide 28
Overflow - Add two positive numbers to get a negative number or two negative numbers to get a positive number
-1 -2 -3 -4 -5 -6 -7 -8
1110 1101 1100 1011 1111
+0
0000 0001 0010 0011 0100 0101 0110
-1 +1 +2 +3 +4 +5 -6 -7 -8 -7 - 2 = +7 -3 -2
1110
1101 1100 1011 1010 1001 1000 1111
+0
0000 0001 0010 0011 0100 0101 0110 0111
+1 +2 +3 +4 +5
-4
-5
1010
1001 1000 0111
+6
+6
+7 5 + 3 = -8
+7
Data Representation
slide 29
Overflow Conditions
5 3 -8 Overflow 5 2 7 No overflow 0000 0101 0010 0111
0111 0101
0011 1000
-7 -2 7 Overflow
-3
-5 -8
No overflow
Overflow when carry-in to the high-order bit does not equal carry out
Data Representation slide 30
15
15
+
0
0
+
0
0
0
0
0
1
1
0
0
1
0
1
0
1
8
23
1
0
1
0
1
0
1
0
1
0
0
1
0
1
0
1
248 (-8)
7
Carry = 0
1
Overflow = 0
1
Carry = 1
1 1
Overflow = 0
1 1 0
0 0 0
0 0 0
1 0 1
1 0 1
1 0 1
1 0 1
79
1 0 1
0 0 1
1 1 1
1 1 0
0 1 1
1 0 1
+
0 1 64 143 (-113)
+
1 0
Carry = 0
Data Representation
Overflow = 1
Carry = 1
Overflow = 1
slide 32
Character Storage
Character sets
Standard ASCII: 7-bit character codes (0 127) Extended ASCII: 8-bit character codes (0 255) Unicode: 16-bit character codes (0 65,535) Unicode standard represents a universal character set
Defines codes for characters used in all major languages Used in Windows-XP: each character is encoded as 16 bits
Null-terminated String
Array of characters followed by a NULL character
Data Representation slide 34
9 A B C D E F
! " #
0 1 2 3 @ A B C P Q R S ` a b c
p q r s 7 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)
Data Representation slide 35
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