02 - Data Representation 2
02 - Data Representation 2
Outline
Positional Number Systems
Binary and Hexadecimal Numbers
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
Positional Number Systems
Different Representations of Natural Numbers
Characters
Most Least
Floating-point numbers Significant Bit Significant Bit
Some common
powers of 2
Convert Unsigned Decimal to
Binary
Repeatedly divide the decimal integer by 2
Each remainder is a binary digit in the translated value
37 = (100101)2
Example:
E B 1 6 A 7 9 4
1110 1011 0001 0110 1010 0111 1001 0100
Converting Hexadecimal to
Decimal
Multiply each digit by its corresponding power of 16
Examples:
stop when
quotient is zero
Double Word 64
carry 1 1 1 1
0 0 1 1 0 1 1 0 (54)
+ 0 0 0 1 1 1 0 1 (29)
0 1 0 1 0 0 1 1 (83)
bit position: 7 6 5 4 3 2 1 0
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
1C37286A A + B = 10 + 11 = 21
+
9395E84B Since 21 ≥ 16
Sum = 21 – 16 = 5
AFCD10B5 Carry = 1
Signed Integers
Several ways to represent a signed number
Sign-Magnitude
Biased
1's complement
2's complement
Divide the range of values into 2 equal parts
First part corresponds to the positive numbers (≥ 0)
Second part correspond to the negative numbers (< 0)
Focus will be on the 2's complement representation
Has many advantages over other representations
Used widely in processors to represent signed integers
Two's Complement Representation
Positive numbers 8-bit Binary Unsigned Signed
value value value
Signed value = Unsigned value
00000000 0 0
Negative numbers 00000001 1 +1
Signed value = Unsigned value – 2n 00000010 2 +2
n = number of bits ... ... ...
0 = positive 1 1 1 1 0 1 1 0
Negative
0 0 0 0 1 0 1 0 Positive
Add 1
Examples:
borrow: 1 1 1 carry: 1 1 1 1
01001101 01001101
– +
00111010 11000110 (2's
complement)
Practice: What is the range of signed values that may be stored in 20 bits?
Carry and Overflow
Carry is important when …
Adding or subtracting unsigned integers
Indicates that the unsigned sum is out of range
Either < 0 or >maximum unsigned n-bit value
Overflow is important when …
Adding or subtracting signed integers
Indicates that the signed sum is out of range
Overflow occurs when
Adding two positive numbers and the sum is negative
Adding two negative numbers and the sum is positive
Can happen because of the fixed number of sum bits
Carry and Overflow Examples
We can have carry without overflow and vice-versa
Four cases are possible (Examples are 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
Range, Carry, Borrow, and
Overflow
Unsigned Integers: n-bit representation
Borrow = 1 Carry = 1
Finite Set of Unsigned Integers
Subtraction Addition
Negative Positive
Finite Set of Signed Integers
Overflow Overflow
Null-terminated String
Array of characters followed by a NULL character
Printable ASCII Codes
0 1 2 3 4 5 6 7 8 9 A B C D E F
2 space ! " # $ % & ' ( ) * + , - . /
3 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
4 @ A B C D E F G H I J K L M N O
5 P Q R S T U V W X Y Z [ \ ] ^ _
6 ` a b c d e f g h i j k l m n o
7 p q r s t u v w x y z { | } ~ DEL
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)
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
Floating Point Arithmetic
Outline
Floating-Point Numbers
S Exponent Fraction
Floating-Point Numbers
S E F = f1 f2 f3 f4 …
Solution:
Sign = 1 is negative
Exponent = (01111100)2 = 124, E – bias = 124 – 127 = –3
Significant = (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?
01000001001001100000000000000000
Solution: implicit
Value in decimal = +(1.01001100 … 0)2 × 2130–127 =
(1.01001100 … 0)2 × 23 = (1010.01100 … 0)2 = 10.375
Examples of Double Precision Float
What is the decimal value of this Double Precision float ?
01000000010100101010000000000000
00000000000000000000000000000000
Solution:
Value of exponent = (10000000101)2 – Bias = 1029 – 1023 = 6
Value of double float = (1.00101010 … 0)2 × 26 (1. is implicit) =
(1001010.10 … 0)2 = 74.5
What is the decimal value of ?
10111111100010000000000000000000
00000000000000000000000000000000
Operation Result
n / 0
x
nonzero / 0
+ (similar for -)
0 / 0 NaN
- NaN (similar for -)
/ NaN
x 0 NaN
NaN op anything NaN
Floating-Point Comparison
IEEE 754 floating point numbers are ordered
Because exponent uses a biased representation …
Exponent value and its binary representation have same ordering
Placing exponent before the fraction field orders the magnitude
Larger exponent larger magnitude
For equal exponents, Larger fraction larger magnitude
0 < (0.F)2 × 2Emin < (1.F)2 × 2E–Bias < ∞ (Emin = 1 – Bias)
Because sign bit is most significant quick test of signed <