Integer Representation
Integer Representation
Number Representation
COMP375 Computer Organization and dA Architecture hit t
Could base state on value of voltage, but control and detection circuits more complex.
compare turning on a light switch to measuring or regulating voltage
COMP375
Number Representations
Integer Numbers
Integers are almost universally stored as bi binary numbers. b When you enter an integer from the keyboard, software converts the ASCII or Unicode characters to a binary integer integer.
Integers
Weighted positional notation
like decimal numbers: 329 3 is worth 300, because of its position, while 9 is only worth 9
most significant least significant
329
102 101 100
101
21
22
20
Binary Fractions
Each position is twice the value of the position iti t to th the right. i ht 23 8 1 22 4 0 21 2 1 20 1 0 . . . 2-1 2-2 2-3
+
Binary Arithmetic
Base-2 addition just like base-10
add from right to left, left propagating carry
carry
1111 1 10000
10111 + 111
COMP375
Number Representations
Negative Integers
Almost all systems for storing negative bi binary numbers b set t th the l left ft most t bit (MSB) to indicate the sign of a number.
Signed Magnitude
Negative number are the same as positive g bit set with the sign
Three bit example
Common formats:
Signed Si d Magnitude M it d Ones Complement Twos Complement There There are t two o zeroes, eroes positi positive e and negati negative. e
Ones Complement
Negative number are the logical inverse of positive numbers p
Three bit example
Mathematically positive and negative zero are the same, but they are different bit patterns.
COMP375
Number Representations
Twos Complement
Negative number are the logical inverse of positive numbers p p plus 1.
Three bit example
000 001 010 011 100 101 110 111 -2 + -1 add carry out of sign -3 0 1 2 3 -4 -3 -2 -1
If number is negative,
start with positive number flip every bit (i.e., take the ones complement) then add one
(-5)
(flip)
(copy)
100110000
COMP375
Number Representations
110011
A. -3 B. 33 C -11 C. 11 D. -12 E. -13
33 3 1 1 1 2 1 3
2s Complement Addition
Use normal binary addition regardless of sign. Ignore carry out
01101000 11110000 01011000
(104) (-16) (98)
Subtraction
Negate subtrahend (2nd number) and add. 00001000 - 00000101 00001000 +11111011 00000011 8 -5 8 + -5 3
COMP375
Number Representations
Sign Extension
When moving an integer into a larger register the upper bits must be set to the sign bit. If we just pad with zeroes on the left:
4-bit 0100 (4) 1100 (-4) 8-bit 00000100 00001100
Overflow
If operands are too big, then sum cannot be represented as an n-bit 2s complement number.
carry into sign bit carry out of sign bit
signs of both operands are the same same, and sign of sum is different.
Decimal Numbers
Some systems (i.e. Intel Pentium) support decimal packed or unpacked p format. numbers in p packed decimal uses 4 bit fields for each digit 9375 = 1001,0011,0111,0101 unpacked decimal uses a byte per digit (ASCII) 9375 = 00111001,00110011,00110111,00110101
1. 2. 3. 4.
la ze rg e ro ne ga tiv e nu m be r
R un sf or ev er
COMP375
Number Representations
Number Order
With normal binary numbers, b many bit bits may change from number to number as you count up. 000 001 010 011 100 101 110 111
bits changed 1 2 1 3 1 2 1
Gray Code
With gray code numbers, b only l 1 bit changes from number to number as you count up. 000 001 011 010 110 111 101 100
bits changed 1 1 1 1 1 1 1
COMP375