number systems and computer arithmetic winter 2014 comp 1380 discrete structures i computing science...
DESCRIPTION
TRU-COMP1380 Number Systems3 Unit Learning Objectives Convert a decimal number to binary number. Convert a decimal number to hexadecimal number. Convert a binary number to decimal number. Convert a binary number to hexadecimal number. Convert a hexadecimal number to binary number. Add two binary numbers. Compute the 1’s complement of a binary number. Compute the 2’s complement of a binary number. Understand the 2’s complement representation for negative integers. Subtract a binary number by using the 2’s complement addition. Multiply two binary numbers. Use of left shift and right shift. Binary divisionTRANSCRIPT
Number Systems and Computer Arithmetic
Winter 2014
COMP 1380 Discrete Structures I
Computing ScienceThompson Rivers University
TRU-COMP1380 Number Systems 2
Course Contents Speaking Mathematically Number Systems and Computer Arithmetic Logic and Truth Tables Boolean Algebra and Logic Gates Vectors and Matrices Sets and Counting Probability Theory and Distributions Statics and Random Variables
TRU-COMP1380 Number Systems 3
Unit Learning Objectives Convert a decimal number to binary number. Convert a decimal number to hexadecimal number. Convert a binary number to decimal number. Convert a binary number to hexadecimal number. Convert a hexadecimal number to binary number . Add two binary numbers. Compute the 1’s complement of a binary number. Compute the 2’s complement of a binary number. Understand the 2’s complement representation for negative integers. Subtract a binary number by using the 2’s complement addition. Multiply two binary numbers. Use of left shift and right shift. Binary division
TRU-COMP1380 Number Systems 4
Unit ContentsThe number systems:The decimal systemThe binary systemThe hexadecimal system
Computer Arithmetic:Binary additionRepresentation of negative integersBinary multiplicationBinary divisionRepresentation of fractions
TRU-COMP1380 Number Systems 5
1. Number Systems
The Decimal System Uses 10 digits 0, 1, 2, ..., 9.
Decimal expansion: 83 = 8×10 + 3 4728 = 4×103 + 7×102 + 2×101 + 8×100
84037 = ??? 43.087 = ???
Do you know addition, subtraction, multiplication and division? 1234 + 435.78 1234 – 435.78 1234 × 435.78 1234 / 435.78
TRU-COMP1380 Number Systems 6
The Binary System In computer systems, the most basic memory unit is a bit that contains
0 or 1. The data unit of 8 bits is referred as a byte that is the basic memory
unit used in main memories and hard disks. All data are represented by using binary numbers. Data types such as
text, voice, image and video have no meaning in the data representation.
8 bits are usually used to express English alphabets. A collection of n bits has 2n possible states(, i.e., numbers). Is it true? E.g.,
How many different numbers can you express using 2 bits? How many different numbers can you express using 4 bits? How many different numbers can you express using 8 bits? How many different numbers can you express using 32 bits?
TRU-COMP1380 Number Systems 7
How can we store integers (i.e., positive numbers only) in a computer? E.g.,
A decimal number 329? Is it okay to store 3 chracters ‘3’, ‘2’, and ‘9’ for 329? How do we store characters? 32910 = ???2
TRU-COMP1380 Number Systems 8
Uses two digits 0 and 1.
02 = 0×20 = 010
12 = 1×20 = 110
102 = 1×21 + 0×20 = 210
112 = 1×21 + 1×20 = 310
1002 = 1×22 + 0×21 + 0×20 = 410
1012 = 1×22 + 0×21 + 1×20 = 510
1102 = 1×22 + 1×21 + 0×20 = 610
1112 = 1×22 + 1×21 + 1×20 = 710
10002 = 1×23 + 0×22 + 0×21 + 0×20 = 810
10012 = 1×23 + 0×22 + 0×21 + 1×20 = 910
...TRU-COMP1380 Number Systems 9
Powers of 2
12 = 1×20 = 110
102 = 1×21 + 0×20 = 210
1002 = 1×22 + 0×21 + 0×20 = 410
10002 = 1×23 + 0×22 + 0×21 + 0×20 = 810
1 00002 = 24 = 1610
10 00002 = 25 = 3210
10 000002 = 26 = 6410
TRU-COMP1380 Number Systems 10
1000 00002 = 27 = ???10
1 0000 00002 = 28 = ???10
10 0000 00002 = 29 = ???10
100 0000 00002 = 210 = ???10
1000 0000 00002 = 211 = ???10
1 0000 0000 00002 = 212 = ???10
Can you memorize the above powers of 2?
Converting to decimals 11012 = ???10
1011 00102 = ???10
1011.00102 = ???10
TRU-COMP1380 Number Systems 11
Converting Decimal to Binary Quotient Remainder
23 / 2 11 111 / 2 5 15 / 2 2 12 / 2 1 01 / 2 0 1=>2310 = 101112
27110 = ???2
607110 = ???2
TRU-COMP1380 Number Systems 12
Another similar idea 27110 = ???2
256 < 271 < 512 -> 271 = 256 + 15= 1 0000 00002 + 15
8< 15 < 16 -> 15 = 8 + 7= 10002 + 7
=>271 = 1 0000 00002 + 15
= 1 0000 00002 + 10002 + 7
= 1 0000 00002 + 10002 + 1112
= 1 0000 11112
127110 = ???2
TRU-COMP1380 Number Systems 13
Hexadecimal Number System 010 = 00002 = 016 = 0x0 110 = 00012 = 116 = 0x1 210 = 00102 = 216 = 0x2 310 = 00112 = 316 = 0x3 410 = 01002 = 416 = 0x4 510 = 01012 = 516 = 0x5 610 = 01102 = 616 = 0x6 710 = 01112 = 716 = 0x7 810 = 10002 = 816 = 0x8 910 = 10012 = 916 = 0x9 1010 = 10102 = A16 = 0xA 1110 = 10112 = B16 = 0xB 1210 = 11002 = C16 = 0xC 1310 = 11012 = D16 = 0xD 1410 = 11102 = E16 = 0xE 1510 = 11112 = F16 = 0xF
0x23c9d6ef = ???10 0x23c9d6ef = ???2
14816 = ???10 14816 = ???2
TRU-COMP1380 Number Systems 14
4 bits can be used for a hexadecimal number, 0, ..., F.
Please memorize it!
Converting Decimal to Hexadecimal
Quotient Remainder 328 / 16 = 20 × 16 + 8
20 / 16 = 1 × 16 + 41 / 16 = 0 × 16 + 1=> 32810 = 14816 = ???2
14816 = (1 × 162 + 4 × 161 + 8 × 160)10
19210 = ???16
TRU-COMP1380 Number Systems 15
Converting Binary to Hexadecimal
4DA916 = ???2
1001101101010012 = ???16
= 100 1101 1010 1001= 4DA9
10 11102 = 0x??? = ???10
0100 1110 1011 1001 01002 = 0x??? = ???10
TRU-COMP1380 Number Systems 16
2. Computer Arithmetic
TRU-COMP1380 Number Systems 17
Binary Addition How to add two binary numbers? Let’s consider only unsigned
integers (i.e., zero or positive numbers only) for a while. Just like the addition of two decimal numbers. E.g.,
10010 10010 1111 + 1001 + 1011 + 1
11011 11101 ???
10111 + 111
???
carry
TRU-COMP1380 Number Systems 18
Binary Subtraction How to subtract a binary number? Just like the subtraction of decimal numbers. E.g., 0112 02 02 1000 10 10 10010 10010 10010 -1 -1 -11 -11 -11 1 ?1 ?11 1111
Try: 101010 How to do? 1 34–79=? -101 -10
Is subtraction easier or more difficult than addition? Why?
TRU-COMP1380 Number Systems 19
In the previous slide, 10010 – 11 = 1111 What if we add
00010010+ 111111001 00001110+ 1 00001111
Is there any relationship between 112 and 111111002? The 8-bit 1’s complement of 112 is ??? Switching 0 1 This type of addition is called 1’s complement addition. Find the 8-bit one’s complements of the followings.
11011 -> 00011011 -> 10 ->00000010 -> 101 ->00000101 ->
TRU-COMP1380 Number Systems 20
In the previous slide, 10010 – 11 = 1111 What if we add
00010010+ 111111011 00001111
Is there any relationship between 11 and 11111101? The 8-bit 2’s complement of 11 is ???
2’s complement ≡ 1’s complement + 1 -> 11111100 + 1 = 11111101 This type of addition is called 2’s complement addition. Find the 16-bit two’s complements of the followings.
11011 -> 0000000000011011 -> 10 101
TRU-COMP1380 Number Systems 21
Another example 101010- 101 ???
What if we use 1’s complement addition or 2’s complement addition instead as followings? Let’s use 8-bit representation.
00101010 00101010+ 11111010 + 111110111 00100100 1 00100101
+ 1 00100101
What does this mean? A – B = A + (–B), where A and B are positive Is –B equal to the 1’s complement or the 2’s complement of B?
TRU-COMP1380 Number Systems 22
1’s complement addition
2’s complement addition
Can we use 8-bit 1’s complement addition for 12 – 102 = –12 ? 1 00000001- 10 + 11111101 <- 8-bit 1’s complement of 10 11111110 <- Is this correct? (Is this 1’s complement of 1?)
Let’s use 8-bit 2’s complement addition for 12 – 102. 00000001+ 11111110 <- 2’s complement of 10 11111111 <- Correct? (2’s complement of 1?)
12 – 102 = 12 + (–102)
Subtraction can be converted to addition with negative numbers. Then, how to represent negative binary numbers, i.e., signed integers?
TRU-COMP1380 Number Systems 23
Representation of Negative Binaries
Representation of signed integers 8 or 16 or 32 bits are usually used for integers. Let’s use 8 bits for examples.
The left most bit (called most significant bit) is used as sign. When the MSB is 0, positive integers. When the MSB is 1, negative integers. The other 7 bits are used for integers. What signed integers can be described using the 8 bit representation?
How to represent positive integer 5? 00001001
How about -9? 10001001 is really okay? 00001001 (9) + 10001001 (-9) = 10010010 (-18) It is wrong! We need a different representation for negative integers.
TRU-COMP1380 Number Systems 24
How about -9? 10001001 is really okay? 00001001 (9) + 10001001 (-9) = 10010010 (-18) It is wrong! We need a different representation for negative integers.
What is the 8-bit 1’s complement of 9? 11110110 <- 8-bit 1’s complement of 9 00001001 + 11110110 <- 9 + 8-bit 1’s complement of 9
= 11111111 <- Is it zero? (1’s complement of 0?) What is the 2’s complement of 9?
11110111 <- 8-bit 2’s complement of 9 00001001 + 11110111 <- 9 + 8-bit 2’s complement of 9
= 1 00000000 <- It looks more like zero.
2’s complement representation is used for negative integers.
TRU-COMP1380 Number Systems 25
12 – 102 = 12 + (–102) ???
00000001+ 11111110 <- 2’s complement of 10, i.e., -102
11111111 <- 2’s complement of 1, i.e., -1 (= 1 – 2) 1010102 – 10011012 = 001010102 + (–010011012) ??? 100102 – 112 ??? 102 – 1112 ??? –102 – 12 ???
Is the two’s complement of the two’s complement of an integer the same integer?
What is x when the 8-bit 2’s complement of x is 11111111 11110011 10000001
TRU-COMP1380 Number Systems 26
8-bit representation with 2’s complement 127 01111111 126 01111110 ... ... 2 00000010 1 00000001 0 00000000 -1 11111111 (28 – 1) -2 11111110 -3 11111101 ... ...-127 10000001-128 10000000
The maximum number is ? byte y = 125; y += 4; ?? The minimum number is ? byte x = -126; x -= 5; ?? What if we add the maximum number by 1 ??? What if we subtract the minimum number by 1 ???
TRU-COMP1380 Number Systems 27
+1
+1+1
overflow
overflow
-1-1-1
16-bit representation with 2’s complement... 01111111 11111111... 01111111 11111110... ... 3 00000000 00000011 2 00000000 00000010 1 00000000 00000001 0 00000000 00000000-1 11111111 11111111-2 11111111 11111110-3 11111111 11111101... ...... 10000000 00000001... 10000000 00000000
The maximum number is ? What if we add the maximum number by 1 ??? The minimum number is ? What if we subtract the minimum number by 1 ???
TRU-COMP1380 Number Systems 28
+1
overflow
overflow
-1
+1+1
-1-1
short y = 32767; y++; ???short x = -32767; x--; ???
Note that computers use the 8-bit representation, the 16-bit representation, the 32-bit representation and the 64-bit representation with 2’complement for negative integers.
In programming lanaguages byte, unsigned byte 8-bit short, unsigned short 16-bit int, unsigned int 32-bit long, unsigned long 64-bit
When we use the 32-bit representation with 2’s complement, The maximum number is ? What if we add the maximum number by 1 ??? The minimum number is ? What if we subtract the minimum number by 1 ???
TRU-COMP1380 Number Systems 29
How to convert a negative binary number to decimal? An example of 8-bit representation,
10011001 = ???
TRU-COMP1380 Number Systems 30
Multiplication of Binary Numbers How to multiply two decimal numbers?
E.g., 1001101 × 1 = ??? 1001101 × 10 = ??? 1001101 × 100 = ??? 1001101 × 101 = 1001101 × 10 × 10 + 1001101 × 0 + 1001101 × 1
What if we shift 1001101 left by one bit? 1001 -> 10010
What if we shift 1001101 left by two bits?1001 -> 100100
Multiplication by a power of 2.
TRU-COMP1380 Number Systems 31
Division of Binary Numbers Binary division? 1111 <- quotient
101 1001101 -101 1001 -101 1000 -101 111 -101 10 <- remainder Try 1101011 / 110 Dividing negative binary numbers: division without sign, and then put the
sign.
TRU-COMP1380 Number Systems 32
Binary division by a power of 2?
1001101 / 10 = 1001101001101 / 100 = 100111001101 / 1000 = 1001
What if we shift 1001101 right by 1 bit? What if we shift 1001101 right by 2 bits? What if we shift 1001101 right by 3 bits?
1001101 / 101 ??? Complicated implementation required
TRU-COMP1380 Number Systems 33
Fractions: Fixed-Point How can we represent fractions?
Use “binary point” to separate positive from negative powers of two -- like “decimal point.”
2’s complement addition and subtraction still work. (Assuming binary points are aligned)
TRU-COMP1380 Number Systems 34
00101000.101 (40.625)+ 11111110.110 (-1.25) (2’s complement)00100111.011 (39.375)
2-1 = 0.52-2 = 0.252-3 = 0.125
No new operations -- same as integer arithmetic.
How to convert decimal fractions to binary? 0.62510 = ???2
0.625 * 2 = 1.25 -> 1 (0.625 = x 2-1 + y 2-2 + z 2-3 + ...) 0.25 * 2 = 0.5 -> 0 (1.25 = x + y 2-1 + z 2-2 + ... => x = 1) 0.5 * 2 = 1.0 -> 1 (0.25 = y 2-1 + z 2-2 + ... ) Therefore 0.101
0.710 = ???2
0.7 * 2 = 1.4 -> 1 0.4 * 2 = 0.8 -> 0 0.8 * 2 = 1.6 -> 1 0.6 * 2 = 1.2 -> 1 0.2 * 2 = 0.4 -> 0 0.4 * 2 = 0.8 -> 0 0.8 * 2 = 1.6 -> 1 ... Therefore 0.1011001...
TRU-COMP1380 Number Systems 35
How to deal with big numbers and small numbers?
Very Large and Very Small: Floating-Point
Large values: 6.023 × 1023 -- requires 79 bits Small values: 6.626 × 10-34 -- requires > 110 bits How to handle those big/small numbers? Use equivalent of “scientific notation”: F × 2E
Need to represent F (fraction), E (exponent), and sign. 6.023 × 1023 = 0.6023 × 1024 6.626 × 10-34 = 0.6626 × 10-33 00101000.101 (40.62510) = 0.101000101 × 26. Store 6 in the exponent and
101000101 in mantissa. (Try the multiplication) IEEE 754 Floating-Point Standard (32-bits):
TRU-COMP1380 Number Systems 36
S Exponent Fraction (mantissa)
1b 8 bits 23 bits