Computer Math. The Decimal System How do we represent “One Hundred and Twenty Five”? How do we...
-
Upload
gwendolyn-marshall -
Category
Documents
-
view
223 -
download
2
Transcript of Computer Math. The Decimal System How do we represent “One Hundred and Twenty Five”? How do we...
Computer MathComputer Math
The Decimal System
How do we represent “One Hundred and How do we represent “One Hundred and Twenty Five”?Twenty Five”?
Simple: 125 !!!Simple: 125 !!! 100s 10s 1s100s 10s 1s
1 2 51 2 5 Which digit is “More Significant”? Why?Which digit is “More Significant”? Why? Answer: the left most digit Answer: the left most digit
How Decimal System Works
This may look simple but please pay This may look simple but please pay attention!!!attention!!!
Decimal is based on ten digits from 0 ~ 9Decimal is based on ten digits from 0 ~ 9 Every time there is an overrun from 0 to 9, Every time there is an overrun from 0 to 9,
the digit to the left of the overrun is the digit to the left of the overrun is increased by a value of 1.increased by a value of 1.
Very simple for most people.Very simple for most people.
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), Since there are only two digits (symbols),
the overrun comes very quickly. the overrun comes very quickly. E.g. zero is ‘0’, one is ‘1’. After ‘one’ is E.g. zero is ‘0’, one is ‘1’. After ‘one’ is
‘two’ but you must write it as ‘1 0’ (see ‘two’ but you must write it as ‘1 0’ (see next pg).next pg).
0 zero0 zero
1 one1 one
10 two 10 two
11 three 11 three
100 four100 four
101 five101 five
How do we represent the number “twelve” How do we represent the number “twelve” in binary?in binary?
8 4 2 18 4 2 1
1 1 0 01 1 0 0 The number on top is the ‘weight’ of the The number on top is the ‘weight’ of the
digit, so you can see that as you move to the digit, so you can see that as you move to the left, the ‘weight’ is multiplied by 2, thus left, the ‘weight’ is multiplied by 2, thus giving rise to the name bi-nary.giving rise to the name bi-nary.
We can see that to represent the number We can see that to represent the number ‘twelve’ requires four binary digits.‘twelve’ requires four binary digits.
Sometimes, instead of saying ‘digits’ we Sometimes, instead of saying ‘digits’ we say ‘bits’ thus 1100 is a 4-bit binary number say ‘bits’ thus 1100 is a 4-bit binary number and has a value of 12 (decimal).and has a value of 12 (decimal).
How many numbers can we represent if we How many numbers can we represent if we have 8 bits?have 8 bits?
How many bits do we need if we only want How many bits do we need if we only want to represent numbers ranging from 0 to 15?to represent numbers ranging from 0 to 15?
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 Write the representation of two hundred thirty
three in binary. three in binary. Ans: 11101001Ans: 11101001
Adding and Subtracting Binary Numbers Adding the numbers is easy as long as we Adding the numbers is easy as long as we
have enough number of bits to represent the have enough number of bits to represent the number. What do we mean by that?number. What do we mean by that?
E.g. For a 4-bit system, the biggest number E.g. For a 4-bit system, the biggest number is 15 so as long as the result does not is 15 so as long as the result does not exceed 15 the system works fine. exceed 15 the system works fine.
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 . 1 0 0 0 . 8 8
0 + 0 = 00 + 0 = 0
0 + 1 = 10 + 1 = 1
1 + 1 = 0 (carry 1) 1 + 1 = 0 (carry 1)
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 01 0 0 0
11 1 0 0 0 +1 0 0 0 +
0 0 0 0 ! 0 0 0 0 !
There is a carry out from the left which There is a carry out from the left which cannot be placed into the answer because cannot be placed into the answer because we only have 4 bits!we only have 4 bits!
Of course the solution is to have more bits Of course the solution is to have more bits but every number of bits has a limit to the but every number of bits has a limit to the biggest number it can store.biggest number it can store.
2’s Complement
Take a binary number, change all ‘1’ to ‘0’ Take a binary number, change all ‘1’ to ‘0’ and all ‘0’ to ‘1’ and you have the 1’s and all ‘0’ to ‘1’ and you have the 1’s complement.complement.
Then add ‘1’ to the right most bit and you Then add ‘1’ to the right most bit and you get the 2’s complement.get the 2’s complement.
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 1 0 1 1 0 1 1 1 2’s comp. 2’s comp.
Simple right?Simple right?
Subtraction of Binary Numbers
This is a bit tricky and you need to know This is a bit tricky and you need to know how to represent signed numbers using 2’s how to represent signed numbers using 2’s complement.complement.
Lets say we have a number ‘12’ in 8-bit Lets say we have a number ‘12’ in 8-bit format.format.
The binary representation is 00001100The binary representation is 00001100 To say that this is +ve 12, we simply use the To say that this is +ve 12, we simply use the
left most bit (which is ‘0’) for that purpose.left most bit (which is ‘0’) for that purpose.
00 0 0 0 1 1 0 0 12 0 0 0 1 1 0 0 12
signsign
Which means that now, there are only seven Which means that now, there are only seven bits representing the number since one of bits representing the number since one of the bits is now used to indicate the sign.the bits is now used to indicate the sign.
To represent a negative 12, get the positive To represent a negative 12, get the positive 12 first,12 first,
0 0 0 0 1 1 0 00 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 11 1 1 1 0 0 1 1 Add ‘1’ to it and we getAdd ‘1’ to it and we get 1 1 1 1 0 0 1 11 1 1 1 0 0 1 1 + 1+ 1 1 1 1 1 0 1 0 01 1 1 1 0 1 0 0 11110100 is (–12) in 2’s comp form.11110100 is (–12) in 2’s comp form.
The left most bit is a ‘1’ and is used to The left most bit is a ‘1’ and is used to indicate that the number is a negative indicate that the number is a negative number.number.
111110100 is (–12) in 2’s comp form.1110100 is (–12) in 2’s comp form.
SignSign
Similarly, only 7 bits are now used to represent Similarly, only 7 bits are now used to represent the rest of the number.the rest of the number.
There is another method using the ‘true value’ Get the positive number 00001100Get the positive number 00001100 Assign the sign bit Assign the sign bit 10001100 10001100 That is what we call the ‘true value’ for –12That is what we call the ‘true value’ for –12 Leave the sign alone and invert the restLeave the sign alone and invert the rest
we get 11110011we get 11110011 Add a ‘1’ to it Add a ‘1’ to it 1 1 We get the 2’s comp. We get the 2’s comp. 1111010011110100
How to subtract?
We know that 24-12 = 12We know that 24-12 = 12 How do we prove this in binary?How do we prove this in binary? The binary of 24 is 00011000The binary of 24 is 00011000 The binary of 12 is 00001100The binary of 12 is 00001100 Simply take the binary (24 signed) and add Simply take the binary (24 signed) and add
it to binary (-12 signed).it to binary (-12 signed).
24 is 00011000 24 is 00011000 +24 is still 00011000 +24 is still 00011000 12 is 0000110012 is 00001100 -12 is 11110100 -12 is 11110100
By adding them, we get: 00011000 By adding them, we get: 00011000
carrycarry 11 1111010011110100
00001100 00001100
We get the answer 12!We get the answer 12!
Hexadecimal System
Now, lets come to the main reason for all Now, lets come to the main reason for all the previous notes.the previous notes.
In MicroControllers, we work a lot with In MicroControllers, we work a lot with machine codes or machine language, which machine codes or machine language, which is basically binary codes in nature. But as is basically binary codes in nature. But as human beings, we are not really human beings, we are not really comfortable working with binary.comfortable working with binary.
To take an intermediate view, let’s look at how we To take an intermediate view, let’s look at how we can use the hexa number system to help us.can use the hexa number system to help us.
Binary ----------------------Base 2Binary ----------------------Base 2 Decimal --------------------Base 10Decimal --------------------Base 10 Hexa ------------------------Base 16Hexa ------------------------Base 16 So we start from 0 ~ 9,A,B,C,D,E,FSo we start from 0 ~ 9,A,B,C,D,E,F ‘‘A’ has a value of 10A’ has a value of 10 ‘‘B’ has a value of 11B’ has a value of 11 ‘‘F’ has a value of 15F’ 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.
What’s the advantage?
Remember, how many bits do we need to Remember, how many bits do we need to represent a number from 0 to 15 (slide 6)represent a number from 0 to 15 (slide 6)
Yes, 4 bits.Yes, 4 bits. Microprocessors/Microcontrollers often Microprocessors/Microcontrollers often
work in 8 bits, 16 bits, 32 bits which are work in 8 bits, 16 bits, 32 bits which are simply groups of 4 bits.simply groups of 4 bits.
So, to make things simple
Instead of writing 11000110 which means Instead of writing 11000110 which means 198 in decimal, we could split it up into 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 And represent it using hexadecimal, which is
written as written as C 6C 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.
Hexadecimal number is usually written with Hexadecimal number is usually written with a subscript 16 e.g.a subscript 16 e.g.
C6C61616 to indicate base-16 to indicate base-16
Or sometimes with a H e.g.Or sometimes with a H e.g.
C6HC6H to indicate hexa to indicate hexa
Or sometimes Or sometimes 0xC60xC6
These are all different ways to represent These are all different ways to represent hexadecimal numbers.hexadecimal numbers.
Another look at 2’s Complement
What is the biggest hex number that can be What is the biggest hex number that can be represented using 8 binary bits?represented using 8 binary bits?
Ans: FF i.e. 1 1 1 1 1 1 1 1Ans: FF i.e. 1 1 1 1 1 1 1 1 If you have only 8 bits, what is the number If you have only 8 bits, what is the number
after FF?after FF? Ans: 00Ans: 00
0000 0000 000000 0000 00 1111 1111 FF 1111 1111 FF 1111 1110 FE1111 1110 FE 1111 1101 FD 1111 1101 FD
0000 0001 010000 0001 01 0000 0000 000000 0000 00
Which means that after FF, the whole cycle Which means that after FF, the whole cycle repeats itself.repeats itself.
If we consider decimal numbers and only If we consider decimal numbers and only limited to 2 digits, we would also encounter limited to 2 digits, we would also encounter the same problem, i.e. from 0 ~ 99 then the same problem, i.e. from 0 ~ 99 then back to 0 again.back to 0 again.
In dealing with signed numbers, what In dealing with signed numbers, what number comes before zero?number comes before zero?
Ans: -1! i.e. negative one.Ans: -1! i.e. negative one.
How do we make the systems consistent? So somehow if after FF comes 00, then FF So somehow if after FF comes 00, then FF
must have a value of –1 in signed must have a value of –1 in signed arithmetic!arithmetic!
What happens if we treat FF as a 2’s What happens if we treat FF as a 2’s complement number?complement number?
What number does it represent?What number does it represent?
To do that,
FF is 1111 1111FF is 1111 1111 The leftmost ‘1’ is the sign indicator.The leftmost ‘1’ is the sign indicator. To find the To find the true valuetrue value using the reverse using the reverse
process, first keep the sign bit and reverse process, first keep the sign bit and reverse all the rest of the bits.all the rest of the bits.
We first get We first get 1000 00001000 0000 Add one to the numberAdd one to the number
1 0 0 0 0 0 0 01 0 0 0 0 0 0 0
11
Ans: Ans: 1 0 0 0 0 0 0 11 0 0 0 0 0 0 1
This is the This is the true valuetrue value for ‘negative one’ for ‘negative one’
That means FF is –1 if we consider signed That means FF is –1 if we consider signed arithmetic. Does it make sense?arithmetic. Does it make sense?
Pls do this: Prove that FE is –2.Pls do this: Prove that FE is –2.