Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Math. The Decimal System How do we represent “One Hundred and Twenty Five”? How do we represent “One Hundred and Twenty Five”? Simple: 125 !!!

Similar presentations


Presentation on theme: "Computer Math. The Decimal System How do we represent “One Hundred and Twenty Five”? How do we represent “One Hundred and Twenty Five”? Simple: 125 !!!"— Presentation transcript:

1 Computer Math

2 The Decimal System How do we represent “One Hundred and Twenty Five”? How do we represent “One Hundred and Twenty Five”? Simple: 125 !!! Simple: 125 !!! 100s 10s 1s 100s 10s 1s 1 2 5 1 2 5 Which digit is “More Significant”? Why? Which digit is “More Significant”? Why? Answer: the left most digit Answer: the left most digit

3 How Decimal System Works This may look simple but please pay attention!!! This may look simple but please pay attention!!! Decimal is based on ten digits from 0 ~ 9 Decimal is based on ten digits from 0 ~ 9 Every time there is an overrun from 0 to 9, the digit to the left of the overrun is increased by a value of 1. Every time there is an overrun from 0 to 9, the digit to the left of the overrun is increased by a value of 1. Very simple for most people. Very simple for most people.

4 The Binary System Based on only 2 digits : ‘0’ and ‘1’ Based on only 2 digits : ‘0’ and ‘1’ Same idea as decimal. Same idea as decimal. Since there are only two digits (symbols), the overrun comes very quickly. Since there are only two digits (symbols), the overrun comes very quickly. E.g. zero is ‘0’, one is ‘1’. After ‘one’ is ‘two’ but you must write it as ‘1 0’ (see next pg). E.g. zero is ‘0’, one is ‘1’. After ‘one’ is ‘two’ but you must write it as ‘1 0’ (see next pg).

5 0 zero 0 zero 1 one 1 one 10 two 10 two 11 three 11 three 100 four 100 four 101 five 101 five

6 How do we represent the number “twelve” in binary? How do we represent the number “twelve” in binary? 8 4 2 1 8 4 2 1 1 1 0 0 1 1 0 0 The number on top is the ‘weight’ of the digit, so you can see that as you move to the left, the ‘weight’ is multiplied by 2, thus giving rise to the name bi-nary. The number on top is the ‘weight’ of the digit, so you can see that as you move to the left, the ‘weight’ is multiplied by 2, thus giving rise to the name bi-nary. We can see that to represent the number ‘twelve’ requires four binary digits. We can see that to represent the number ‘twelve’ requires four binary digits.

7 Sometimes, instead of saying ‘digits’ we say ‘bits’ thus 1100 is a 4-bit binary number and has a value of 12 (decimal). Sometimes, instead of saying ‘digits’ we say ‘bits’ thus 1100 is a 4-bit binary number and has a value of 12 (decimal). How many numbers can we represent if we have 8 bits? How many numbers can we represent if we have 8 bits? How many bits do we need if we only want to represent numbers ranging from 0 to 15? How many bits do we need if we only want to represent numbers ranging from 0 to 15?

8 Review Questions. Review Questions. Where does the word ‘binary’ come from? Where does the word ‘binary’ come from? How many symbols are used in a binary system? How many symbols are used in a binary system? Where does the word ‘decimal’ come from? Where does the word ‘decimal’ come from? How many symbols are used in a decimal system? How many symbols are used in a decimal system? Write the representation of two hundred thirty three in binary. Write the representation of two hundred thirty three in binary. Ans: 11101001 Ans: 11101001

9 Adding and Subtracting Binary Numbers Adding the numbers is easy as long as we have enough number of bits to represent the number. What do we mean by that? Adding the numbers is easy as long as we have enough number of bits to represent the number. What do we mean by that? E.g. For a 4-bit system, the biggest number is 15 so as long as the result does not exceed 15 the system works fine. E.g. For a 4-bit system, the biggest number is 15 so as long as the result does not exceed 15 the system works fine.

10 Lets add 5 to 3 Lets add 5 to 3 0 1 0 1 0 1 0 1 0 0 1 1 + 0 0 1 1 + 1 0 0 0. 8 1 0 0 0. 8 0 + 0 = 0 0 + 1 = 1 1 + 1 = 0 (carry 1)

11 However if you add 8 to 8 For a 4-bit system you will have a problem. For a 4-bit system you will have a problem. 1 0 0 0 1 0 0 0 1 1 0 0 0 + 1 1 0 0 0 + 0 0 0 0 ! 0 0 0 0 ! There is a carry out from the left which cannot be placed into the answer because we only have 4 bits! There is a carry out from the left which cannot be placed into the answer because we only have 4 bits!

12 Of course the solution is to have more bits but every number of bits has a limit to the biggest number it can store. Of course the solution is to have more bits but every number of bits has a limit to the biggest number it can store.

13 2’s Complement Take a binary number, change all ‘1’ to ‘0’ and all ‘0’ to ‘1’ and you have the 1’s complement. Take a binary number, change all ‘1’ to ‘0’ and all ‘0’ to ‘1’ and you have the 1’s complement. Then add ‘1’ to the right most bit and you get the 2’s complement. Then add ‘1’ to the right most bit and you get the 2’s complement.

14 Example : 01001001 0 1 0 0 1 0 0 1 original no. 0 1 0 0 1 0 0 1 original no. 1 0 1 1 0 1 1 0 1’s comp. 1 0 1 1 0 1 1 0 1’s comp. + 1 + 1 1 0 1 1 0 1 1 1 2’s comp. 1 0 1 1 0 1 1 1 2’s comp. Simple right? Simple right?

15 Subtraction of Binary Numbers This is a bit tricky and you need to know how to represent signed numbers using 2’s complement. This is a bit tricky and you need to know how to represent signed numbers using 2’s complement. Lets say we have a number ‘12’ in 8-bit format. Lets say we have a number ‘12’ in 8-bit format. The binary representation is 00001100 The binary representation is 00001100 To say that this is +ve 12, we simply use the left most bit (which is ‘0’) for that purpose. To say that this is +ve 12, we simply use the left most bit (which is ‘0’) for that purpose.

16 0 0 0 0 1 1 0 0 12 0 0 0 0 1 1 0 0 12 sign sign Which means that now, there are only seven bits representing the number since one of the bits is now used to indicate the sign. Which means that now, there are only seven bits representing the number since one of the bits is now used to indicate the sign.

17 To represent a negative 12, get the positive 12 first, To represent a negative 12, get the positive 12 first, 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 Perform 2’s complement on it and we get: Perform 2’s complement on it and we get: 1 1 1 1 0 0 1 1 1 1 1 1 0 0 1 1 Add ‘1’ to it and we get Add ‘1’ to it and we get 1 1 1 1 0 0 1 1 1 1 1 1 0 0 1 1 + 1 + 1 1 1 1 1 0 1 0 0 1 1 1 1 0 1 0 0 11110100 is (–12) in 2’s comp form. 11110100 is (–12) in 2’s comp form.

18 The left most bit is a ‘1’ and is used to indicate that the number is a negative number. The left most bit is a ‘1’ and is used to indicate that the number is a negative number. 11110100 is (–12) in 2’s comp form. 11110100 is (–12) in 2’s comp form.Sign Similarly, only 7 bits are now used to represent the rest of the number. Similarly, only 7 bits are now used to represent the rest of the number.

19 There is another method using the ‘true value’ Get the positive number 00001100 Get the positive number 00001100 Assign the sign bit 10001100 Assign the sign bit 10001100 That is what we call the ‘true value’ for –12 That is what we call the ‘true value’ for –12 Leave the sign alone and invert the rest Leave the sign alone and invert the rest we get 11110011 we get 11110011 Add a ‘1’ to it 1 Add a ‘1’ to it 1 We get the 2’s comp. 11110100 We get the 2’s comp. 11110100

20 How to subtract? We know that 24-12 = 12 We know that 24-12 = 12 How do we prove this in binary? How do we prove this in binary? The binary of 24 is 00011000 The binary of 24 is 00011000 The binary of 12 is 00001100 The binary of 12 is 00001100 Simply take the binary (24 signed) and add it to binary (-12 signed). Simply take the binary (24 signed) and add it to binary (-12 signed).

21 24 is 00011000 24 is 00011000 +24 is still 00011000 +24 is still 00011000 12 is 00001100 12 is 00001100 -12 is 11110100 -12 is 11110100 By adding them, we get: 00011000 carry 1 11110100 carry 1 11110100 00001100 00001100 We get the answer 12!

22 Hexadecimal System Now, lets come to the main reason for all the previous notes. Now, lets come to the main reason for all the previous notes. In MicroControllers, we work a lot with machine codes or machine language, which is basically binary codes in nature. But as human beings, we are not really comfortable working with binary. In MicroControllers, we work a lot with machine codes or machine language, which is basically binary codes in nature. But as human beings, we are not really comfortable working with binary.

23 To take an intermediate view, let’s look at how we can use the hexa number system to help us. To take an intermediate view, let’s look at how we can use the hexa number system to help us. Binary ----------------------Base 2 Binary ----------------------Base 2 Decimal --------------------Base 10 Decimal --------------------Base 10 Hexa ------------------------Base 16 Hexa ------------------------Base 16 So we start from 0 ~ 9,A,B,C,D,E,F So we start from 0 ~ 9,A,B,C,D,E,F ‘A’ has a value of 10 ‘A’ has a value of 10 ‘B’ has a value of 11 ‘B’ has a value of 11 ‘F’ has a value of 15 ‘F’ has a value of 15 Altogether 0 ~ F there are 16 symbols. Altogether 0 ~ F there are 16 symbols. Thus Base-16. Thus Base-16.

24 What’s the advantage? Remember, how many bits do we need to represent a number from 0 to 15 (slide 6) Remember, how many bits do we need to represent a number from 0 to 15 (slide 6) Yes, 4 bits. Yes, 4 bits. Microprocessors/Microcontrollers often work in 8 bits, 16 bits, 32 bits which are simply groups of 4 bits. Microprocessors/Microcontrollers often work in 8 bits, 16 bits, 32 bits which are simply groups of 4 bits.

25 So, to make things simple Instead of writing 11000110 which means 198 in decimal, we could split it up into Instead of writing 11000110 which means 198 in decimal, we could split it up into 1100 0110 (2 sets of 4 bits) 1100 0110 (2 sets of 4 bits) And represent it using hexadecimal, which is written as And represent it using hexadecimal, which is written as C 6 C 6 Remember 1100 has a value of 12 whereas Remember 1100 has a value of 12 whereas 0110 has a value of 6. 0110 has a value of 6.

26 Hexadecimal number is usually written with a subscript 16 e.g. Hexadecimal number is usually written with a subscript 16 e.g. C6 16 to indicate base-16 C6 16 to indicate base-16 Or sometimes with a H e.g. Or sometimes with a H e.g. C6H to indicate hexa C6H to indicate hexa Or sometimes 0xC6 Or sometimes 0xC6 These are all different ways to represent hexadecimal numbers. These are all different ways to represent hexadecimal numbers.

27 Another look at 2’s Complement What is the biggest hex number that can be represented using 8 binary bits? What is the biggest hex number that can be represented using 8 binary bits? Ans: FF i.e. 1 1 1 1 1 1 1 1 Ans: FF i.e. 1 1 1 1 1 1 1 1 If you have only 8 bits, what is the number after FF? If you have only 8 bits, what is the number after FF? Ans: 00 Ans: 00

28 0000 0000 00 0000 0000 00 1111 1111 FF 1111 1111 FF 1111 1110 FE 1111 1110 FE 1111 1101 FD 1111 1101 FD 0000 0001 01 0000 0001 01 0000 0000 00 0000 0000 00

29 Which means that after FF, the whole cycle repeats itself. Which means that after FF, the whole cycle repeats itself. If we consider decimal numbers and only limited to 2 digits, we would also encounter the same problem, i.e. from 0 ~ 99 then back to 0 again. If we consider decimal numbers and only limited to 2 digits, we would also encounter the same problem, i.e. from 0 ~ 99 then back to 0 again. In dealing with signed numbers, what number comes before zero? In dealing with signed numbers, what number comes before zero? Ans: -1! i.e. negative one. Ans: -1! i.e. negative one.

30 How do we make the systems consistent? So somehow if after FF comes 00, then FF must have a value of –1 in signed arithmetic! So somehow if after FF comes 00, then FF must have a value of –1 in signed arithmetic! What happens if we treat FF as a 2’s complement number? What happens if we treat FF as a 2’s complement number? What number does it represent? What number does it represent?

31 To do that, FF is 1111 1111 FF is 1111 1111 The leftmost ‘1’ is the sign indicator. The leftmost ‘1’ is the sign indicator. To find the true value using the reverse process, first keep the sign bit and reverse all the rest of the bits. To find the true value using the reverse process, first keep the sign bit and reverse all the rest of the bits. We first get 1000 0000 We first get 1000 0000 Add one to the number Add one to the number

32 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 Ans: 1 0 0 0 0 0 0 1 This is the true value for ‘negative one’ This is the true value for ‘negative one’ That means FF is –1 if we consider signed arithmetic. Does it make sense? That means FF is –1 if we consider signed arithmetic. Does it make sense? Pls do this: Prove that FE is –2. Pls do this: Prove that FE is –2.


Download ppt "Computer Math. The Decimal System How do we represent “One Hundred and Twenty Five”? How do we represent “One Hundred and Twenty Five”? Simple: 125 !!!"

Similar presentations


Ads by Google