Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University
TRU-COMP1380 Number Systems2 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 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 division
TRU-COMP1380 Number Systems4 Unit Contents The number systems: The decimal system The binary system The hexadecimal system Computer Arithmetic: Binary addition Representation of negative integers Binary multiplication Binary division Representation of fractions
TRU-COMP1380 Number Systems5 1. Number Systems
The Decimal System Uses 10 digits 0, 1, 2,..., 9. Decimal expansion: 83 = 8× = 4× × × × = ??? = ??? Do you know addition, subtraction, multiplication and division? – × / TRU-COMP1380 Number Systems6
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 2 n 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 Systems7
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? = ??? 2 TRU-COMP1380 Number Systems8
Uses two digits 0 and = 0×2 0 = = 1×2 0 = = 1× ×2 0 = = 1× ×2 0 = = 1× × ×2 0 = = 1× × ×2 0 = = 1× × ×2 0 = = 1× × ×2 0 = = 1× × × ×2 0 = = 1× × × ×2 0 = TRU-COMP1380 Number Systems9
Powers of = 1×2 0 = = 1× ×2 0 = = 1× × ×2 0 = = 1× × × ×2 0 = = 2 4 = = 2 5 = = 2 6 = TRU-COMP1380 Number Systems10
= 2 7 = ??? = 2 8 = ??? = 2 9 = ??? = 2 10 = ??? = 2 11 = ??? = 2 12 = ??? 10 Can you memorize the above powers of 2? Converting to decimals = ??? = ??? = ??? 10 TRU-COMP1380 Number Systems11
Converting Decimal to Binary QuotientRemainder 23 / / / / / 201 => = = ??? = ??? 2 TRU-COMP1380 Number Systems12
Another similar idea = ??? = = = = => 271 = = = = = ??? 2 TRU-COMP1380 Number Systems13
Hexadecimal Number System 0 10 = = 0 16 = 0x = = 1 16 = 0x = = 2 16 = 0x = = 3 16 = 0x = = 4 16 = 0x = = 5 16 = 0x = = 6 16 = 0x = = 7 16 = 0x = = 8 16 = 0x = = 9 16 = 0x = = A 16 = 0xA = = B 16 = 0xB = = C 16 = 0xC = = D 16 = 0xD = = E 16 = 0xE = = F 16 = 0xF 0x23c9d6ef = ??? 10 0x23c9d6ef = ??? = ??? = ??? 2 TRU-COMP1380 Number Systems14 4 bits can be used for a hexadecimal number, 0,..., F. Please memorize it!
Converting Decimal to Hexadecimal QuotientRemainder 328 / 16 = 20 × / 16 = 1 × / 16 = 0 × => = = ??? = (1 × × × 16 0 ) = ??? 16 TRU-COMP1380 Number Systems15
Converting Binary to Hexadecimal 4DA9 16 = ??? = ??? 16 = = 4DA = 0x??? = ??? = 0x??? = ??? 10 TRU-COMP1380 Number Systems16
2. Computer Arithmetic TRU-COMP1380 Number Systems17
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., ??? ??? carry TRU-COMP1380 Number Systems18
Binary Subtraction How to subtract a binary number? Just like the subtraction of decimal numbers. E.g., ?1 ? Try: How to do? 1 34–79=? Is subtraction easier or more difficult than addition? Why? TRU-COMP1380 Number Systems19
In the previous slide, – 11 = 1111 What if we add Is there any relationship between 11 2 and ? The 8-bit 1’s complement of 11 2 is ???Switching 0 1 This type of addition is called 1’s complement addition. Find the 8-bit one’s complements of the followings > > 10 -> > 101 -> > TRU-COMP1380 Number Systems20
In the previous slide, – 11 = 1111 What if we add Is there any relationship between 11 and ? The 8-bit 2’s complement of 11 is ??? 2’s complement ≡ 1’s complement + 1 -> = This type of addition is called 2’s complement addition. Find the 16-bit two’s complements of the followings > > TRU-COMP1380 Number Systems21
Another example ??? What if we use 1’s complement addition or 2’s complement addition instead as followings? Let’s use 8-bit representation 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 Systems22 1’s complement addition 2’s complement addition
Can we use 8-bit 1’s complement addition for 1 2 – 10 2 = –1 2 ? <- 8-bit 1’s complement of <- Is this correct? (Is this 1’s complement of 1?) Let’s use 8-bit 2’s complement addition for 1 2 – <- 2’s complement of <- Correct? (2’s complement of 1?) 1 2 – 10 2 = (–10 2 ) Subtraction can be converted to addition with negative numbers. Then, how to represent negative binary numbers, i.e., signed integers? TRU-COMP1380 Number Systems23
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? How about -9? is really okay? (9) (-9) = (-18) It is wrong! We need a different representation for negative integers. TRU-COMP1380 Number Systems24
How about -9? is really okay? (9) (-9) = (-18) It is wrong! We need a different representation for negative integers. What is the 8-bit 1’s complement of 9? <- 8-bit 1’s complement of < bit 1’s complement of 9 = <- Is it zero? (1’s complement of 0?) What is the 2’s complement of 9? <- 8-bit 2’s complement of < bit 2’s complement of 9 = <- It looks more like zero. 2’s complement representation is used for negative integers. TRU-COMP1380 Number Systems25
1 2 – 10 2 = (–10 2 ) ??? <- 2’s complement of 10, i.e., <- 2’s complement of 1, i.e., -1 (= 1 – 2) – = (– ) ??? – 11 2 ??? 10 2 – ??? –10 2 – 1 2 ??? 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 TRU-COMP1380 Number Systems26
8-bit representation with 2’s complement (2 8 – 1) 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 Systems27 +1 overflow
16-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 Systems28 +1 overflow +1 short y = 32767; y++; ??? short x = ; 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 byte8-bit short,unsigned short16-bit int,unsigned int32-bit long,unsigned long64-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 Systems29
How to convert a negative binary number to decimal? An example of 8-bit representation, = ??? TRU-COMP1380 Number Systems30
Multiplication of Binary Numbers How to multiply two decimal numbers? E.g., × 1 = ??? × 10 = ??? × 100 = ??? × 101 = × 10 × × × 1 What if we shift left by one bit? > What if we shift left by two bits? > Multiplication by a power of 2. TRU-COMP1380 Number Systems31
Division of Binary Numbers Binary division? 1111 <- quotient <- remainder Try / 110 Dividing negative binary numbers: division without sign, and then put the sign. TRU-COMP1380 Number Systems32
Binary division by a power of 2? / 10 = / 100 = / 1000 = 1001 What if we shift right by 1 bit? What if we shift right by 2 bits? What if we shift right by 3 bits? / 101 ??? Complicated implementation required TRU-COMP1380 Number Systems33
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 (40.625) (-1.25) (2’s complement) (39.375) 2 -1 = = = No new operations -- same as integer arithmetic.
How to convert decimal fractions to binary? = ??? * 2 = > 1(0.625 = x y z ) 0.25 * 2 = 0.5 -> 0(1.25 = x + y z => x = 1) 0.5 * 2 = 1.0 -> 1(0.25 = y z ) Therefore = ??? * 2 = 1.4 -> * 2 = 0.8 -> * 2 = 1.6 -> * 2 = 1.2 -> * 2 = 0.4 -> * 2 = 0.8 -> * 2 = 1.6 -> 1... Therefore TRU-COMP1380 Number Systems35 How to deal with big numbers and small numbers?
Very Large and Very Small: Floating-Point Large values: × requires 79 bits Small values: × requires > 110 bits How to handle those big/small numbers? Use equivalent of “scientific notation”: F × 2 E Need to represent F (fraction), E (exponent), and sign × = × × = × ( ) = × 2 6. Store 6 in the exponent and in mantissa. (Try the multiplication) IEEE 754 Floating-Point Standard (32-bits): TRU-COMP1380 Number Systems36 SExponentFraction (mantissa) 1b 8 bits 23 bits