Computer Organization
&
Design
Number System
Department of Electrical and Computer Engineering
Missouri University of Science & Technology
[email protected]
1
Decimal Numbers: Base 10
• Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• Example:
3271 =
• (3x10 ) + (2x10 ) + (7x10 ) + (1x10 )
3 2 1 0
Numbers: positional notation
• Number Base B => B symbols per digit:
• Base 10 (Decimal): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Base 2 (Binary): 0, 1
• Number representation:
• d31d30 ... d2d1d0 is a 32-digit number
• value = d31x B31 + d30 x B30 + ... + d2 x B2 + d1 x B1 + d0 x B0
• Binary: 0,1
• 1011010 = 1x26 + 0x25 + 1x24 + 1x23 + 0x22 + 1x21 + 0x20
= 64 + 16 + 8 + 2 = 90
• Notice that a 7-digit binary number converts into a 2-digit decimal number
• Which base(s) convert(s) to binary easily?
Hexadecimal Numbers: Base 16
• Example (convert hex to decimal):
• B28F0DD = (Bx166) + (2x165) + (8x164) + (Fx163) + (0x162) +
(Dx161) + (Dx160)
= (11x166) + (2x165) + (8x164) + (15x163) + (0x162) +
(13x161) + (13x160)
= 187232477 decimal
• Notice that a 7-digit hex number is converted to a 9-digit
decimal number
Decimal vs. Hexadecimal vs.Binary
•Examples: 00 0 0000
01 1 0001
•1010 1100 0101 (binary) 02 2 0010
= AC5 (hex)
03 3 0011
04 4 0100
05 5 0101
•10111 (binary) 06 6 0110
= 0001 0111 (binary) 07 7 0111
= 17 (hex) 08 8 1000
09 9 1001
10 A 1010
11 B 1011
•3F9(hex) 12 C 1100
= 0011 1111 1001 (binary) 13 D 1101
14 E 1110
15 F 1111
Hex to Binary Conversion
• HEX is a more compact representation of binary.
• Each hex digit represents 16 decimal values.
• Four binary digits represent 16 decimal values.
• Therefore, each hex digit can replace four binary digits.
• Example:
0011 1011 1001 1010 1100 1010 0000 0000 binary
3b 9 a c a 0 0 hex
Which Base Should We Use?
• Decimal: Great for humans; most arithmetic is done with
this base.
• Binary: This is what computers use, so get used to them.
Become familiar with how to do basic arithmetic with
them (+,-,*,/).
• Hex: Terrible for arithmetic; but if we are looking at long
strings of binary numbers, it is much easier to convert
them to hex and look at four bits at a time.
What can we do with binary representations of
numbers?
• Everything we can do with decimal numbers.
• Addition 1 1
• Subtraction 1 0 1 0
• Multiplication + 0 1 1 1
• Division
• Comparison -------------------------
• Example: 10 + 7 = 17 1 0 0 0 1
• so simple to add in binary that we can build circuits to do it
• subtraction also just as in decimal
Signed-magnitude representation
• In decimal: +98, -10, +0, -0
• In binary, the Most Significant Bit (MSB) (leftmost bit) is dedicated as the
sign bit.
• MSB = 0 for positive numbers
• MSB = 1 for negative numbers
• 8-bit examples: 01010101 = + 85(10)
11010101 = - 85(10)
• Range: -(2n-1 – 1) through +(2n-1 – 1)
• With 8 bits: -127 through +127
• Two representations of zero
Complement number systems
• Assumptions are the following:
• Fixed number of digits: n
• Radix is r
• Integers of form: D = d d …d d
n-1 n-2 1 0
• If the result of an operation produces a number that needs more than n
digits, we discard the higher-order digits
• If D is complemented twice, the result is D
• - (-D) = D
Radix-complement notation
• Complement of n-digit D is -D = r – D n
• If 1 ≤ D ≤ 2 – 1, then 1 ≤ -D ≤ 2 – 1
n n
• 0’ = 2 , which is n+1 bits long: 100000000
n
• Per convention, we discard the MSB
• Results in only one representation of 0
2’s-complement notation
• -D = 2 n – D = ((2n-1) – D) + 1
• 2 -1 has the form 11111111
n
• 1-1 = 0; 1-0 = 1 toggles each bit
• Toggle every bit to get ((2 -1)–D)
n
• Add 1 to result to get 2’s complement
2’s-complement notation
• A number is negative iff its MSB is 1
• When converting to decimal, everything is the
same, except weight of MSB for a negative number
is -(2n-1) instead of +(2n-1)
• Range: -(2n-1) through +(2n-1 – 1)
• For 8 bits: -128 through 127
15
Examples
• 85(10) = 01010101; toggle bits:
10101010
+1 add 1;
10101011 = - 85(10)
• Check: -128 + 32 + 8 + 2 + 1 = -85
• -99(10) = 10011101; toggle bits:
01100010
______+1; add 1
01100011 = 99(10)
• Check: 64 + 32 + 2 + 1 = 99
2’s complement addition
• Just like decimal, but per convention, ignore carry out
of MSB
• Result will be correct unless range is exceeded
(overflow)
• Overflow only happens when two numbers being
added have the same sign
2’s complement addition
• Recall that range for 8 bits: -128 through 127
01111111 = 127(10)
+ 00000001 =
10000000 = -128(10) incorrect result
We expected 128, which cannot be represented with 8 digits
(out of range)
18
Overflow
10000000 = -128(10
+ 11111111 = -1(10)
1 01111111 = 127(10) incorrect result
We expected -129, which cannot be represented with 8
digits (out of range)
Overflow
• Check for overflow
• Do both addends have the same sign?
• If no, overflow is impossible.
• If yes, does the sum have the same sign as them? If it
does not, then overflow.
• Other method:
• If carry into MSB ≠ carry out of MSB; then overflow
20
2’s complement subtraction
• Turn it into an addition by negating the subtrahend
(+4) – (+3) = (+4) + (-3) = +1
0100
+ 1101
1 0001
(+3) – (+4) = (+3) + (-4) = -1
0011
+ 1100
1111
2’s complement subtraction
• Shortcut: To negate the second number, we toggle the bits
and add 1 to the result. Since we will eventually be adding
two numbers, we can combine this addition with the final
one.
• Toggle bits of the second number (minuend), and add to
the first, with a carry-in of 1.
22
Overflow detection
• For overflow detection, check the signs of the two numbers
being added, and the sign of the result. This is exactly the
same as before.
• Or: If carry into MSB ≠ carry out of MSB; then overflow
• (-8)-(+1) = -9 overflow is expected
1000
+ 1111
1 0111
2’s complement of a non-integer
• Definition is the same as for integers:
• Complement of n-digit D is -D = rn – D
• Here, n refers to the number of digits to the left of the decimal point (integer digits)
• Example: D = 010.11
• Number of integer digits = n = 3
• -D = 2n – D = 23 – D = 1000 – 010.11
1000.00
+ 010.11
101.01
Decimal codes
• Binary numbers are most appropriate for internal
operations of a computer.
• External interfaces (I/O) may read or display decimal, for
the benefit of humans.
• Logical conclusion is that we need an easy way of
representing decimal numbers with bits.
• A coded representation of the 10 digits of the decimal
number system (0-9) is known as a binary-coded decimal
(BCD) representation.
Some definitions
• Code: a set of n-bit strings, where each string represents a
different number, letter, or other thing.
• Code word: one such n-bit string.
• A legal, or valid code word, is one that is actually used to
represent something.
• With n bits, we can have 2 code words, but not all of these are
n
necessarily used to represent something. Some of them may be
unused.
• Example: A BCD code needs to represent 10 digits (0-9)
• At least 4 bits are needed to represent 10 things
• 4 bits give us 16 possible code words
• 10 of these 16 are legal code words
• 6 are unused
Binary coded decimal (BCD)
• Most natural representation is to use 4-bit strings, where
each decimal digit is represented its binary representation
• 0000 through 1001 is used to represent the decimal digits 0
through 9, respectively.
• This is the 8421 BCD scheme, which is a weighted code.
• To convert from decimal to BCD, replace each decimal
digit with its BCD 4-bit string.
Binary coded decimal (BCD)
• Keep in mind that this BCD number is NOT the same as you
would get if converting decimal to binary the usual way.
• Example: BCD string for 16 is 0001 0110. Binary equivalent
of 16 is 0001 0000.
• 2 BCD digits (one byte) can represent 0 through 99.
• A normal byte can represent 0 to 255 (unsigned), or -128 to
127 (signed).
Unit-distance codes
• Useful for when an analog quantity needs to be converted
to digital.
• Only one bit can change as successive integers are coded.
• Gray code is a common example.
4-bit Gray code
Decimal number Gray code
0 0000
1 0001
2 0011
3 0010
4 0110
5 0111
6 0101
7 0100
8 1100
9 1101
10 1111
11 1110
12 1010
13 1011
14 1001
15 1000
Why is it useful?
• Assume that the position of a shaft, which is an analog
quantity, needs to be digitally represented.
• A positional encoder wheel is attached to the shaft.
• Accuracy provided by 4 binary digits is sufficient.
Alphanumeric codes
• Alphabetic information also needs to be handled by digital
systems.
• Need to represent letters of the alphabet in upper and
lowercase, numbers, punctuation marks, symbols such as $
and @, and control operations such as Backspace and
Carriage Return.
• The best known alphanumeric code is the 7-bit American
Standard Code for Information Exchange (ASCII).
• A more recent code, the Unicode Standard, uses 16-bit
strings and codes characters from foreign languages as
well. Also includes codes for math symbols, etc.