Number Systems
Z. Jerry ShiDepartment of Computer Science and EngineeringDepartment of Computer Science and Engineering
University of Connecticut
CSE2300W: Digital Logic Design
Number systems
• Roman numerals:I 1II 2III 3IV 4V 5X 10XVII 17L 50C 100D 500M 1000…MMVI 2006
Roman numerals
I (1), V(5), X(10), L(50), C(100), D (500), M(1000)• Rules
F l f i h– From left to right– The largest numeral possible is used first– A symbol is not repeated more than four times
Placing a small numeral in front of a large numeral indicates subtraction– Placing a small numeral in front of a large numeral indicates subtraction• The smaller one must be a power of 10 (I, X, or C)• Only one numeral each time
• To represent years:2010 : MMX 2763 : MMDCCLXIII1998 : MCMXCVIII 2751 : MMDCCLIN b f f h f di f R (4/21/753 BC)Number of years from the founding of Rome (4/21/753 BC)
• Very difficult to represent large numbers• Arithmetic is tough• Arithmetic is tough
A matchstick puzzle: I + XI = X ?
Arabic system
• Arabic is a type of positional system– The value a digit represents depends on its positionThe value a digit represents depends on its position
• In Roman system, X is always 10. So XX = 20.
• Ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9• A digit’s value depends on its position
1712 = 1000 + 700 + 10 + 21712 1000 + 700 + 10 + 2
…x3x2x1x0.x-1… = 103 + 102 + 101 + 100 + 10 1… x3103 + x2102 + x1101 + x0100 + x-110-1 …
Here, the base is 10. We have ten digits, The value of a digit increases 10 times per position when it is moved to the left.
Generalization
The base (or radix) r does not have to be 10It can be many other numbers Different number systemsy y
Suppose the radix of a number system is r >= 2.E h i i diff l i i di iEach position may r different values, requiring r digits.
The value of an n-digit radix r number isThe value of an n digit radix r number is
xn-1 xn-2…x2x1x0 = xn-1rn-1+ xn-2rn-2+…+ x2r2 + x1r1 + x0r0
0 <= xi < r Each digit has r choices.
Common number systems (numeral systems)
Decimal system: r = 10, ten digits: 0…9Binary system: r = 2 two digits: 0 or 1 (bit)Binary system: r 2, two digits: 0 or 1 (bit)Octal system: r = 8, eight digits: 0…7Hexadecimal system: r = 16, 16 digits: 0…9A…Fy g
A16 = 1010, B16 = 1110, C16 = 1210, 16 10,D16 = 1310, E16 = 1410, F16 = 1510, 16 10,
Examples
Decimal system: r = 10, ten digits: 0…9Binary system: r = 2, two digits: 0 or 1y y , g
…x3x2x1x0… = … x323 + x222 + x121 + x020 …
1001 2= 1×23 + 0×22 + 0×21 + 1×201×2 + 0×2 + 0×2 + 1×2= 8 + 0 + 0 + 1 = 9
0xABCD
Question
• There are so many number systems, …
• How do I know which system to use? – Use the most convenient one– Have to meet all constraints
b diff ?• How can I convert a number to a different system?
Number conversion (1)
(xn-1xn-2…x2x1x0)r
= x 1rn-1+ x 2rn-2+…+ x2r2 + x1r1 + x0r0 xn-1r xn-2r … x2r x1r x0r
Convert xi and r into the new systemDo addition and multiplication in the new system
Assume you can do arithmetic in the destination systemy yFor you, decimal system is the convenientFor computers, binary system is the most convenient
To decimal numbers
You can convert numbers of any radix to decimal numbers
(1001)2= (1×23 + 0×22 + 0×21 + 1×20)10
8 0 0 1= 8 + 0 + 0 + 1 = 9
0xAB76 = 10 × 163 + 11 × 162 + 7 × 16 + 6 = 43894
Another way
(xn-1xn-2…x2x1x0)r
= x rn-1+ x rn-2+ + x r2 + x r1 + x r0= xn-1r + xn-2r +…+ x2r + x1r + x0r= (…(xn-1r+ xn-2)r+xn-3)r…+ x2)r + x1)r + x0
(1001)2
= (((1×2 + 0)×2) + 0)×2 + 1(((1×2 + 0)×2) + 0)×2 + 1= 9
0xAB76 = 10 × 163 + 11 × 162 + 7 × 16 + 6
((10 × 16) 11) × 16 7 ) × 16 6= ((10 × 16) + 11) × 16 + 7 ) × 16 + 6
If you are not familiar with the destination system, …
But you know the arithmetic in the source system.Assume the new radix is s.
V = (xn-1xn-2…x2x1x0)r = (ym-1ym-2…y2y1y0)s= (y 1y 2 y2y1) ×s + y (ym-1ym-2…y2y1)s ×s + y0
And y0 < sYou can compute y0 first: y0 = V mod s.
How about y1 ?(V – y0) / s = (ym-1ym-2…y2y1)s = (ym-1ym-2…y2)s ×s + y1
Then compute y2 : y2 = (V / s2) mod s.and so on… and so on
Example: convert 78 to a binary number
26 = 64 25 = 32 24 = 16 23 = 8 22 = 4 21 = 2 20 = 1
1 2 4 9 19 39 78/2 = 0 /2 = 1 /2=2 /2 = 4 /2 = 9 /2 = 19 / 2 = 39/2 = 0 /2 = 1 /2=2 /2 = 4 /2 = 9 /2 = 19 / 2 = 391 0 0 1 1 1 0
(78) (1001110)(78)10 = (1001110)2
Summary
A number in radix r system to radix s system• If you know how to compute in the destination systemIf you know how to compute in the destination system, …
• If you know how to compute in the source system, …y p y
• If it is difficult to compute in either system, …
Binary to hexadecimal
• Method 1:– Convert the number to a decimal number firstConvert the number to a decimal number first
• Method 2:– Divide digits into groups of four– Convert each group to a hex digit
1110 0110 0001E 6 1
Hexadecimal to binary
Convert each digit to a 4-digit binary number
(E61)16 = (1110 0110 0001)2
(ABCD)16 = (1010101111001101)2( )16 ( )2
How about octal numbers? binary to octal : divide digits into groups of 3octal to binary: convert each digit to a 3-digit binary numberoctal to binary: convert each digit to a 3 digit binary number
Why can I do that?
(1110 0110 0001)2
= 1 × 211 + 1 × 210 + 1 × 29 + 0 × 28 +1 2 + 1 2 + 1 2 + 0 2 +0 × 27 + 1 × 26 + 1 × 25 + 0 × 24 +0 × 23 + 0 × 22 + 0 × 21 + 1 × 20
= (1 × 23 + 1 × 22 + 1 × 21 + 0 × 20) ×28 +(0 × 23 + 1 × 22 + 1 × 21 + 0 × 20 ) ×24 +(0 × 23 + 0 × 22 + 0 × 21 + 1 × 20) ×20
= (1 × 23 + 1 × 22 + 1 × 21 + 0 × 20) ×162 +(0 23 + 1 22 + 1 21 + 0 20 ) 161 +(0 × 23 + 1 × 22 + 1 × 21 + 0 × 20 ) ×161 +(0 × 23 + 0 × 22 + 0 × 21 + 1 × 20) ×160
= (E61)= (E61)16
Shortcut methods
A number in radix r system to radix s system• If s = rmIf s r ,
– Divide the digits into groups of m– Convert each group to a digit in the destination system
• If r = sm,– Convert each digit to a m-digit number in the destination system
Exercise: Convert 45 to binary, octal, and hex numbers.
How can I do the conversion faster?
• Memorize powers of 2• Memorize the binary representation of simple numbersMemorize the binary representation of simple numbers• Practice
– Do not use calculators for small numbers ( < 1000)– You are not allowed to use calculators in exams
Jan 8, 2010 news
• Pi buster: French software whiz claims record– Fabrice Bellard calculated Pi to nearly 2.7 trillion decimal placesy p– Around 123 billion digits more than the previous record (2.577 trillion
decimal digits) set in August 2009 by Japanese professor Daisuke Takahashi
• Took 29 hours on T2K open supercomputer– It took 131 days on a computer that costs less than $3,000
• 103 for the computation in binary digits103 for the computation in binary digits• 13 days for verification• 12 days to convert the binary digits to a decimal • Three final days to check the conversionThree final days to check the conversion
Addition and subtraction of numbers
Decimal numbers Binary numbers
6 5 3 9
7 1 6 3+
1 0 1 1
0 1 0 1
0 1 1 1+
0 1 1 11 0 1 1
3 7 0 2
0 1 1 1
1 1 0 0
Addition table for binary numbers
Cin X Y Cout SCin X Y Cout S0 0 0 0 0 0 0 1 0 1 0 1 0 0 10 1 0 0 10 1 1 1 0 1 0 0 0 11 0 1 1 0 1 1 0 1 0 1 1 1 1 11 1 1 1 1
Additions of binary numbers
Subtraction of binary numbers
1 0 0 1
0 1 1 10 1 1 1−0 1 1 0
0 0 1 0
Addition and subtraction of hex numbers
1 9 B 9
Hex numbers Hex numbers
E 1 9 F1 9 B 9
C 7 E 6+
0 1 1 0
E 1 9 F
1 9 B 9−0 1 1 00 1 1 0
E 1 9 F
0 1 1 0
C 7 E 6
Which system are these numbers in?
34 + 23 = 112
(3r + 4) + (2r + 3) = (r2 + r + 2)r2 – 4r – 5 = 0
r = 5 or r = – 1or r 1
You may also make an educated guess:4 + 3 = 7 = c × r + 2c is either 0 or 1 for addition
Negative numbers: signed-magnitude
• For non-negative numbers, n bits can present 2n numbers: 0, 1, 2, …, 2n – 1,
• Signed-magnitude representation– A bit indicates the sign
• 0 = positive, 1 = negative– There are two 0’s: +0 and – 0– n bits represent 2n – 1 numbers:n bits represent 2 1 numbers:
– ( 2n – 1 – 1), …, – 1, 0, 1, 2, …, 2n – 1 – 1 – Arithmetic is difficult
Addition of signed-magnitude numbers
// each number has a sign and a magnitude (mag)Add (x, y)
if x.sign == y.sign // 5 + 3, (-4) + (-2)sum.sign = x.signsum.mag = x.mag + y.mag
else if x.mag > y.mag // 5 + (-2), -4 + 3 sum.sign = x.signsum.mag = x.mag – y.mag
else // (-2) + 5, 3 + (-4)sum.sign = y.signsum.mag = y.mag – x.mag
// another ‘if’ statement can remove -0. // How about subtraction?
Complement number systems
• We deal with a fixed number of digits, say, n digits• We focus on radix complement number
Ten’s complement for decimal numbers (r = 10)– Ten s complement for decimal numbers (r = 10)– Two’s complement for binary numbers (r = 2)
• Easy to perform additon/subtraction• The complement of an n-digit number is rn – xThe complement of an n digit number is r x
– The complement of rn – x is x
Example: 3-digit ten’s complement numbersunsigned: 000, 001, 002, …, 999
The complement of 1 is 999 = 1000 – 1The complement of 2 is 998 = 1000 – 2The complement of 3 is 997 = 1000 – 3…The complement of 999 is 1 = 1000 – 999The complement of 0 is 000 = 1000 – 0
Changing the radix to 2
• r = 2, n = 8
• rn = 256• r = 256
Unsigned numbers: 0, 1, 2, …, 254, 255Unsigned numbers: 0, 1, 2, …, 254, 255The complements: 0, 255, 254, …, 2, 1
How to compute the complements?Either in decimal system or binary system1 0000 0000 – 1111 1010 = 1111 1111 – 1111 1010 + 1
1) Complement every bit and 2) add 1 to the flipped bits
3-bit two’s complement numbers
Bits Binary Two’s complement
The complement of 1 : – 1The complement of 2 : – 2
000 0
001 1
0
1
The complement of 2 : 2 The complement of 3 : – 3 The complement of 4 : – 4
010 2
011 3
2
3
How do you tell the sign of a two’s complement number?
100 4
101 5
– 4
3
How do you change the sign of a two’s complement
number?101 5
110 6
– 3
– 2
number?
111 7 – 1
Two’s complement numbers
n bitsUnsigned: 0, 1, 2, …, 2n–1 – 1, 2n–1 , 2n–1+1, …, 2n – 1g , , , , , , , ,Two’s complement: – 2n–1 … – 1,0, 1, 2, …, 2n–1 – 1
n = 8: 128 1 0 127n = 8: – 128,…, – 1, 0, …, 127n = 9: – 256, …, – 1, 0, …, 255
Consider a value xIf 0 ≤ x ≤ 2n–1 – 1,
x has the same representations (left-most bit is 0)If – 2n–1 ≤ x ≤ – 1,
x is represented with the complement of |x|x is represented with the complement of |x|
8-bit two’s complement numbers
Unsigned: 0, …, 255Two’s complement: –128, –127, …, –1, 0, 1, …127
Representation is the same for 0, .., 127.–128 –1 in two’s complement system are represented as128, …, 1, in two s complement system are represented as 128, …, 255 in unsigned binary system, respectively.
Decimal Abs. value Complement Bits
–128 128 128 = 256 – 128 1000 0000–127 127 129 = 256 – 127 1000 0001–127 127 129 256 – 127 1000 0001
–2 2 254 = 256 – 2 1111 1110–1 1 255 = 256 – 1 1111 1111
Convert decimal numbers to two’s complement numbers
Make sure you can represent the number.n bits: – 2n–1 to 2n–1 – 1
To find out the 2’s complement representation of x:
If x ≥ 0, it is the same as unsigned binary representation.If x < 0 it is the unsigned binary representation of the complement of |x|If x < 0, it is the unsigned binary representation of the complement of |x|.
You can find out the complement of |x| either in decimal system or in binary system.
Example: x = (– 119) n = 8Example: x = (– 119), n = 8. 1. Represent |x| as 8-bit binary number: |x| = 11910 = 0111 011122. Find out the complement of |x|:
Complement all the bits: 1000 10002Add 1 to the complemented bits: 1000 1001Add 1 to the complemented bits: 1000 10012
Another way:1. Find out the complement of |x| in decimal system. It is 256 – 119 = 137.2. Convert 137 to an 8-bit binary number.
Line to circle
0000
1001
2010
− 1 111
4100
5101
6110
3011
7111
81000
91001000 001 010111 100 101 110011 111 1000 1001
0000
81000
9− 1 000
1001
7111
1001 1
111
20106
110− 2 111 110
3
111
4100
0115101
Two’s complement numbers and modulo 8 arithmetic
000 – 8 0 8
001 – 7 1 9
010 – 6 2 10
011 – 5 3 11
100 – 4 4 12100 4 4 12
101 – 3 5 13
110 2 6 14110 – 2 6 14
111 – 1 7 15
You can consider it is modulo 8 arithmetic
Modular arithmetic
• Modulus: the numbers wrap around after reaching the value– Consider the example of clockConsider the example of clock
• If the modulus is n >= 2, the unique values in the system is n• Two integers a and b are said to be congruent modulo n if their
difference is an integer multiple of na = b mod na = b mod n
3 = 8 = 18 = 103 mod 5• Same rules for negative numbers
– 9 = – 1 = 7 = 15 mod 8• Basically, you do the operation of finding the postive remainder
Convert two’s complement numbers to decimal numbers
• If the sign bit is 0, consider it as an unsigned binary number– A 1 at bit i is 2i
• If the sign bit is 1, it is a negative number• Method 1:
– Find out the complement in binary systemConvert the complement to a decimal number– Convert the complement to a decimal number
– Add the sign• Method 2:
– A 1 at bit i is 2i for i < n – 1 while the weight of bit n – 1 is – 2n – 1
These two methods are essentially the same.
Example: What is the decimal value of the 4-bit two’s complement number 1001?
Method 1: The complement is 0111. So the decimal value is – 7.
Method 2: The value is – 8 + 1 = – 7.
Why two’s complement numbers?
• Same circuit to add unsigned binary numbers and two’s complement numbersp– Follow the same rules
• Subtraction is easy: x – y = x + (– y)– Computing – y is simple: flip bits and add 1
Wheel for unsigned binary nubmers
Wheel for two’s complement numbers
Addition and subtraction of two’s complement numbers
1 1 1 0 2’s complement Binary1 1 1 0
0 0 0 1+
0 0 0 0
p
–2
1
y
14
1
1 1 1 1 –1 15
0101 1001
+ 1001 0010
89
– 110
89
1461001 0010
1110 1011
110
–21
146
235
Overflow
• Addition or subtraction may result in overflow– Results are not in the range– Crossing the lower bound is also called underflow
• Unsigned numbers:Unsigned numbers: – Carry from the highest bit (the most significant bit) indicates an overflow4-bit unsigned numbers: 5 + 13 = 18 5 – 13 = – 8
• Two complement’s numbers– Cin != Cout for the highest bit indicates an overflow
• Sign is not changed– The MSB of two numbers are the same, but the result has a different sign
• Two numbers with different signs do not cause overflowg
When you are coding in C
char a; /* 8-bit, signed */unsigned char b; /* 8-bit, unsigned */g ; , gshort c; /* 16-bit, signed */unsigned short d; /* 16-bit, unsigned */i 0 1 /* 32 bi i d */int e0, e1; /* 32-bit, signed */unsigned int f0, f1; /* 32-bit, unsigned */
/* Read the manual of the compiler you are using */e0 + e1 /* 32-bit addition */f0 + f1 /* 32-bit addition *//* does not matter when you do additions *//* however when you compare values be careful *// however, when you compare values, be careful /
Changing the number of bits in a number
• Add more bits to binary numbers, e.g., 8-bit to 16-bit– Unsigned binary numbers: Add 0’s to the left– Two’s complement numbers: sign extension (duplicate the sign bit)1000 0000 1111 1111 1000 00000010 1010 0000 0000 0010 10100010 1010 0000 0000 0010 1010
• Reduce the number of bits in a binary number, e.g., 16-bit to 8-bit– Unsigned numbers:
• Check whether the removed bits are 0– Two’s complement numbers:
• Check whether the removed bits are the same and whether the sign has been changed during the conversion
Binary code for decimal numbers (BCD)
• Four bits represent a single decimal digit from 0 to 9, instead of 0 to 15
Decimal Binary 0 0000 1 0001– 8 bits to represent 1010. 1 00012 0010 3 0011 4 01005 0101 6 0110 7 0111 8 10009 1001
NOT USED 1010 NOT USED 1011NOT USED 1011NOT USED 1100 NOT USED 1101 NOT USED 1110NOT USED 1110NOT USED 1111
Gray code
• Two adjacent numbers have only one different bit – Named after Frank Gray (Bell Labs)Named after Frank Gray (Bell Labs)
Gray code
• Recursive:One bit : 0 1T o bits: 00 01 11 10Two bits: 00 01 11 10Three bits: 000 001 011 010 110, 111, 101, 100And so on:
Pattern is 0 1 1 0, instead of 0 1 0 1
• Gray code was also known as reflected binary codey y– Repeat lower (n – 1) bits in the reversed order – Add the leading 0 for the first half and 1 for the second half
• Directly from binary code– bit i and (i + 1) are the same, bit i in gray code is 0. It’s 1 otherwise.
0111 0100bi 0 1 d 2 h i h bi d bi 0 d 1 0 i h dbits 0, 1, and 2 are the same in the binary code, so bits 0 and 1 are 0 in the gray code
ASCII (American Standard Code for Information Interchange)
• Each character is represented with 7 bits with a leading 0– How is CSE2300 stored in computer memory?p y
• Many other encoding schemes (such as Unicode)