Difference between revisions of "Negative Numbers"
(→Using Two's Complement) |
(→TRC PowerPoint) |
||
(32 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
+ | =OverView= | ||
+ | Video from 0:00 until 6.25 | ||
+ | |||
+ | <youtube>https://www.youtube.com/watch?v=CglODZZm_Z4&list=PLCiOXwirraUDGCeSoEPSN-e2o9exXdOka&index=3</youtube> | ||
+ | |||
+ | https://www.youtube.com/watch?v=CglODZZm_Z4&list=PLCiOXwirraUDGCeSoEPSN-e2o9exXdOka&index=3 (0:00 - 6:25) | ||
+ | |||
=Two's Complement= | =Two's Complement= | ||
Two's Complement uses a similar number system to binary except the msb or left hand bit is a negative value, meaning for 8 bit two's complement it would be -128 instead of 128 like it is in regular 8bit binary. | Two's Complement uses a similar number system to binary except the msb or left hand bit is a negative value, meaning for 8 bit two's complement it would be -128 instead of 128 like it is in regular 8bit binary. | ||
Line 13: | Line 20: | ||
Two's Complement can be used to convert binary numbers from positive to negative, to do this we need to: | Two's Complement can be used to convert binary numbers from positive to negative, to do this we need to: | ||
− | + | 1) Write the number as its equivalent positive binary form | |
− | + | 2) Add 0's to the number to make it 8 bit | |
− | + | 3) Invert each bit, changing 0's to 1's and 1's to 0's | |
− | + | 4) Add 1 to the number to make it a two's complement number | |
For example, represent -41 in two's complement form: | For example, represent -41 in two's complement form: | ||
− | First calculate +41 in binary using your preferred method | + | First calculate +41 in binary using your preferred method: |
+ | |||
+ | 41 = 32+0+8+0+0+1 = 101001 | ||
− | + | Then add 0's to make it 8 bit: | |
− | + | 00101001 | |
− | Then Invert the bits | + | Then Invert the bits: |
− | + | 11010110 | |
− | + | Then Add 1 to the number: | |
− | 11010111 = -128+64+16+4+2+1 = -41 | + | 11010110 |
+ | + 1 | ||
+ | -------- | ||
+ | 11010111 | ||
+ | -------- | ||
+ | |||
+ | To check our answer we can convert the number to denary, remembering that the msb represent -128: | ||
+ | |||
+ | 11010111 = -128+64+16+4+2+1 = -41 | ||
=Method 2= | =Method 2= | ||
There is one other method for representing numbers in two's complement form, without using calculations. To do this we need to: | There is one other method for representing numbers in two's complement form, without using calculations. To do this we need to: | ||
− | + | 1) Write the number is its equivalent positive binary form | |
− | + | 2) Add 0's to the number to make it 8 bit | |
− | + | 3) Starting from the right and going left find the first 1 and keep it | |
− | + | 4) Invert each bit after, changing 0's to 1's and 1's to 0's, but don't invert the 1 you kept or any 0's to the right of it | |
For example, represent -46 in twos complement: | For example, represent -46 in twos complement: | ||
− | First calculate +46 in binary using your preferred method | + | First calculate +46 in binary using your preferred method: |
+ | |||
+ | 46 = 32+0+8+4+2+0 = 101110 | ||
+ | |||
+ | Then add 0's to make it up to 8bit: | ||
+ | |||
+ | 00101110 | ||
+ | |||
+ | Then find the first one and keep it: | ||
+ | |||
+ | 001011'''1'''0 | ||
+ | ^ | ||
+ | |||
+ | Then invert the bits excluding the 1 you kept and all 0's to the right of it: | ||
+ | |||
+ | 11010010 | ||
+ | |||
+ | To check our answer we can convert the number to denary, remembering that the msb represent -128: | ||
+ | |||
+ | 11010010 = -128+64+16+2= -46 | ||
+ | |||
+ | =Revision Questions= | ||
+ | <quiz display=simple> | ||
+ | |||
+ | { | ||
+ | True or false, the biggest number we can make in twos compliment binary is 128 | ||
+ | | type="()" } | ||
+ | -true | ||
+ | +false | ||
+ | ||We cannot make 128 in twos compliment, as the first bit is equal to -128. If we add up every bit after that, we would only be able to reach 127. | ||
+ | |||
+ | { | ||
+ | What is the difference between the two methods for calculating negative binary values? | ||
+ | | type="()" } | ||
+ | - One involves inverting the bits, one does not | ||
+ | - In one of the methods, you have to make it a 8 bit value. | ||
+ | + For one you have to add 1 to the end of the number, for the other you must invert all the bits aside from the right-most 1. | ||
+ | - One allows you to skip inverting the bits | ||
+ | || In both methods you must invert the bits, which you cannot skip, and it must be a 8bit number. | ||
+ | || In method 1, you must invert all the bits and add 1 to the rightmost digit ; in method 2, you keep the rightmost one, and then invert the rest. | ||
+ | |||
+ | { | ||
+ | What is -34 in 8-bit two's complement binary? | ||
+ | | type="()" } | ||
+ | |||
+ | + 11011110 | ||
+ | - 01011110 | ||
+ | - 10100110 | ||
+ | - 01010111 | ||
+ | || We know the answer must be 11011110 or 10100110, because if the first bit is a 0, the denary would be positive. | ||
+ | || Next, we should calculate what +38 is in binary. This would be 00100010. We can invert the bits to get 11011110 | ||
+ | |||
+ | { | ||
+ | What is the Number range of 8 bit two's compliment in denary? | ||
+ | | type="()" } | ||
+ | |||
+ | - +128 to -128 | ||
+ | - +128 to -127 | ||
+ | + +127 to -128 | ||
+ | - +127 to -127 | ||
+ | || The biggest number you can create in twos compliment is +127 ; 01111111 | ||
+ | || The smaller number you can create is -128 ; 10000000 | ||
+ | || This is because in twos compliment, the left-most digit counts as -128. | ||
− | + | { | |
+ | | type="{}"} | ||
+ | What is the 8-bit two's compliment number 10010110 in denary? | ||
+ | { -106 } | ||
+ | || Firstly, we can invert the numbers to get the positive denary value. The inverted binary would be 01101010. | ||
+ | || 01101010 would be 64+32+8+2. This is 106, which we can flip directly to a negative number to get the answer. | ||
− | + | { | |
+ | | type="{}"} | ||
+ | What is the 8-bit two's compliment number 01110101 in denary? | ||
+ | { 117 } | ||
+ | || We can identify here that the answer will be a positive number, as the first bit is a 0 | ||
+ | || The 1s in this fall on 64, 32, 16, 2, and 8. Adding these up gives us 117. | ||
− | + | { | |
+ | | type="{}"} | ||
+ | What does 'msb' stand for? Please format your answer in lowercase, and check your spelling. | ||
+ | { most significant bit } | ||
− | + | { | |
+ | Which position does the msb hold? | ||
+ | | type="()"} | ||
+ | - The middle | ||
+ | + The leftmost bit | ||
+ | - The rightmost bit | ||
+ | || The msb is the leftmost bit in a sequence. | ||
− | + | { | |
+ | | type="{}"} | ||
+ | What is the 8-bit two's compliment number 10011101 in denary? | ||
+ | { -99 } | ||
+ | || When there is a 1 in the -128 column, the number will always be negative. | ||
− | -128+ | + | { |
+ | | type="{}"} | ||
+ | What is the 8-bit two's compliment binary number 11111111 in denary? | ||
+ | { -1 } | ||
+ | || In this question we can assume the answer to be -1 as the first digit (-128) + every other number (127) would be -1. | ||
+ | </quiz> |
Latest revision as of 08:25, 25 September 2020
OverView
Video from 0:00 until 6.25
https://www.youtube.com/watch?v=CglODZZm_Z4&list=PLCiOXwirraUDGCeSoEPSN-e2o9exXdOka&index=3 (0:00 - 6:25)
Two's Complement
Two's Complement uses a similar number system to binary except the msb or left hand bit is a negative value, meaning for 8 bit two's complement it would be -128 instead of 128 like it is in regular 8bit binary.
-128 64 32 16 8 4 2 1
We can see that a 1 in the msb position, or the position of -128 would result in the binary number being negative as the other bits 64-1 only total 127. This means that even if there was a 1 in every position a two's complement number of 11111111 in binary would equal -1.
this means that in two's complement if the msb is a 0 the number is positive and if it is a 1 the number is negative.
Therefore we know that the smallest possible value in 8bit two's complement binary is 10000000 = -128 and the largest value is 01111111 = +127.
Method 1
Two's Complement can be used to convert binary numbers from positive to negative, to do this we need to:
1) Write the number as its equivalent positive binary form 2) Add 0's to the number to make it 8 bit 3) Invert each bit, changing 0's to 1's and 1's to 0's 4) Add 1 to the number to make it a two's complement number
For example, represent -41 in two's complement form:
First calculate +41 in binary using your preferred method:
41 = 32+0+8+0+0+1 = 101001
Then add 0's to make it 8 bit:
00101001
Then Invert the bits:
11010110
Then Add 1 to the number:
11010110 + 1 -------- 11010111 --------
To check our answer we can convert the number to denary, remembering that the msb represent -128:
11010111 = -128+64+16+4+2+1 = -41
Method 2
There is one other method for representing numbers in two's complement form, without using calculations. To do this we need to:
1) Write the number is its equivalent positive binary form 2) Add 0's to the number to make it 8 bit 3) Starting from the right and going left find the first 1 and keep it 4) Invert each bit after, changing 0's to 1's and 1's to 0's, but don't invert the 1 you kept or any 0's to the right of it
For example, represent -46 in twos complement:
First calculate +46 in binary using your preferred method:
46 = 32+0+8+4+2+0 = 101110
Then add 0's to make it up to 8bit:
00101110
Then find the first one and keep it:
00101110 ^
Then invert the bits excluding the 1 you kept and all 0's to the right of it:
11010010
To check our answer we can convert the number to denary, remembering that the msb represent -128:
11010010 = -128+64+16+2= -46