Presentation is loading. Please wait.

Presentation is loading. Please wait.

Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.

Similar presentations


Presentation on theme: "Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University."— Presentation transcript:

1 Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University

2 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

3 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

4 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

5 TRU-COMP1380 Number Systems5 1. Number Systems

6 The Decimal System Uses 10 digits 0, 1, 2,..., 9. Decimal expansion: 83 = 8×10 + 3 4728 = 4×10 3 + 7×10 2 + 2×10 1 + 8×10 0 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 Systems6

7 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

8 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? 329 10 = ??? 2 TRU-COMP1380 Number Systems8

9 Uses two digits 0 and 1. 0 2 = 0×2 0 = 0 10 1 2 = 1×2 0 = 1 10 10 2 = 1×2 1 + 0×2 0 = 2 10 11 2 = 1×2 1 + 1×2 0 = 3 10 100 2 = 1×2 2 + 0×2 1 + 0×2 0 = 4 10 101 2 = 1×2 2 + 0×2 1 + 1×2 0 = 5 10 110 2 = 1×2 2 + 1×2 1 + 0×2 0 = 6 10 111 2 = 1×2 2 + 1×2 1 + 1×2 0 = 7 10 1000 2 = 1×2 3 + 0×2 2 + 0×2 1 + 0×2 0 = 8 10 1001 2 = 1×2 3 + 0×2 2 + 0×2 1 + 1×2 0 = 9 10... TRU-COMP1380 Number Systems9

10 Powers of 2 1 2 = 1×2 0 = 1 10 10 2 = 1×2 1 + 0×2 0 = 2 10 100 2 = 1×2 2 + 0×2 1 + 0×2 0 = 4 10 1000 2 = 1×2 3 + 0×2 2 + 0×2 1 + 0×2 0 = 8 10 1 0000 2 = 2 4 = 16 10 10 0000 2 = 2 5 = 32 10 10 00000 2 = 2 6 = 64 10 TRU-COMP1380 Number Systems10

11 1000 0000 2 = 2 7 = ??? 10 1 0000 0000 2 = 2 8 = ??? 10 10 0000 0000 2 = 2 9 = ??? 10 100 0000 0000 2 = 2 10 = ??? 10 1000 0000 0000 2 = 2 11 = ??? 10 1 0000 0000 0000 2 = 2 12 = ??? 10 Can you memorize the above powers of 2? Converting to decimals 1101 2 = ??? 10 1011 0010 2 = ??? 10 1011.0010 2 = ??? 10 TRU-COMP1380 Number Systems11

12 Converting Decimal to Binary QuotientRemainder 23 / 2 111 11 / 251 5 / 221 2 / 210 1 / 201 => 23 10 = 10111 2 271 10 = ??? 2 6071 10 = ??? 2 TRU-COMP1380 Number Systems12

13 Another similar idea 271 10 = ??? 2 256 271 = 256 + 15 = 1 0000 0000 2 + 15 8 15 = 8 + 7 = 1000 2 + 7 => 271 = 1 0000 0000 2 + 15 = 1 0000 0000 2 + 1000 2 + 7 = 1 0000 0000 2 + 1000 2 + 111 2 = 1 0000 1111 2 1271 10 = ??? 2 TRU-COMP1380 Number Systems13

14 Hexadecimal Number System 0 10 = 0000 2 = 0 16 = 0x0 1 10 = 0001 2 = 1 16 = 0x1 2 10 = 0010 2 = 2 16 = 0x2 3 10 = 0011 2 = 3 16 = 0x3 4 10 = 0100 2 = 4 16 = 0x4 5 10 = 0101 2 = 5 16 = 0x5 6 10 = 0110 2 = 6 16 = 0x6 7 10 = 0111 2 = 7 16 = 0x7 8 10 = 1000 2 = 8 16 = 0x8 9 10 = 1001 2 = 9 16 = 0x9 10 10 = 1010 2 = A 16 = 0xA 11 10 = 1011 2 = B 16 = 0xB 12 10 = 1100 2 = C 16 = 0xC 13 10 = 1101 2 = D 16 = 0xD 14 10 = 1110 2 = E 16 = 0xE 15 10 = 1111 2 = F 16 = 0xF 0x23c9d6ef = ??? 10 0x23c9d6ef = ??? 2 148 16 = ??? 10 148 16 = ??? 2 TRU-COMP1380 Number Systems14 4 bits can be used for a hexadecimal number, 0,..., F. Please memorize it!

15 Converting Decimal to Hexadecimal QuotientRemainder 328 / 16 = 20 × 16 + 8 20 / 16 = 1 × 16 + 4 1 / 16 = 0 × 16 + 1 => 328 10 = 148 16 = ??? 2 148 16 = (1 × 16 2 + 4 × 16 1 + 8 × 16 0 ) 10 192 10 = ??? 16 TRU-COMP1380 Number Systems15

16 Converting Binary to Hexadecimal 4DA9 16 = ??? 2 100110110101001 2 = ??? 16 = 100 1101 1010 1001 = 4DA9 10 1110 2 = 0x??? = ??? 10 0100 1110 1011 1001 0100 2 = 0x??? = ??? 10 TRU-COMP1380 Number Systems16

17 2. Computer Arithmetic TRU-COMP1380 Number Systems17

18 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., 10010100101111 + 1001 + 1011 + 1 1101111101 ??? 10111 + 111 ??? carry TRU-COMP1380 Number Systems18

19 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 Systems19

20 In the previous slide, 10010 – 11 = 1111 What if we add 00010010 + 11111100 1 00001110 + 1 00001111 Is there any relationship between 11 2 and 11111100 2 ? 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. 11011 ->00011011 -> 10 ->00000010 -> 101 ->00000101 -> TRU-COMP1380 Number Systems20

21 In the previous slide, 10010 – 11 = 1111 What if we add 00010010 + 11111101 1 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 Systems21

22 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 + 11111011 1 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 Systems22 1’s complement addition 2’s complement addition

23 Can we use 8-bit 1’s complement addition for 1 2 – 10 2 = –1 2 ? 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 1 2 – 10 2. 00000001 + 11111110 <- 2’s complement of 10 11111111 <- Correct? (2’s complement of 1?) 1 2 – 10 2 = 1 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

24 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 Systems24

25 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 Systems25

26 1 2 – 10 2 = 1 2 + (–10 2 ) ??? 00000001 + 11111110 <- 2’s complement of 10, i.e., -10 2 11111111 <- 2’s complement of 1, i.e., -1 (= 1 – 2) 101010 2 – 1001101 2 = 00101010 2 + (–01001101 2 ) ??? 10010 2 – 11 2 ??? 10 2 – 111 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 111111111111001110000001 TRU-COMP1380 Number Systems26

27 8-bit representation with 2’s complement 12701111111 12601111110...... 200000010 100000001 000000000 -111111111 (2 8 – 1) -211111110 -311111101...... -12710000001 -12810000000 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

28 16-bit representation with 2’s complement...01111111 11111111...01111111 11111110... 300000000 00000011 200000000 00000010 100000000 00000001 000000000 00000000 -111111111 11111111 -211111111 11111110 -311111111 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 Systems28 +1 overflow +1 short y = 32767; y++; ??? short x = -32767; x--; ???

29 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

30 How to convert a negative binary number to decimal? An example of 8-bit representation, 10011001 = ??? TRU-COMP1380 Number Systems30

31 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 Systems31

32 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 Systems32

33 Binary division by a power of 2? 1001101 / 10 = 100110 1001101 / 100 = 10011 1001101 / 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 Systems33

34 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 Systems34 00101000.101 (40.625) +11111110.110 (-1.25) (2’s complement) 00100111.011 (39.375) 2 -1 = 0.5 2 -2 = 0.25 2 -3 = 0.125 No new operations -- same as integer arithmetic.

35 How to convert decimal fractions to binary? 0.625 10 = ??? 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.7 10 = ??? 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 Systems35 How to deal with big numbers and small numbers?

36 Very Large and Very Small: Floating-Point Large values: 6.023 × 10 23 -- 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 × 2 E Need to represent F (fraction), E (exponent), and sign. 6.023 × 10 23 = 0.6023 × 10 24 6.626 × 10 -34 = 0.6626 × 10 -33 00101000.101 (40.625 10 ) = 0.101000101 × 2 6. Store 6 in the exponent and 101000101 in mantissa. (Try the multiplication) IEEE 754 Floating-Point Standard (32-bits): TRU-COMP1380 Number Systems36 SExponentFraction (mantissa) 1b 8 bits 23 bits


Download ppt "Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University."

Similar presentations


Ads by Google