Difference between revisions of "Negative Numbers"
(→Two's Complement) |
(→Two's Complement) |
||
Line 2: | Line 2: | ||
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. | ||
− | -128 64 32 16 8 4 2 1 | + | -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. | 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. |
Revision as of 18:56, 14 December 2016
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.
Using Two's Complement
Two's Complement can be used to convert binary numbers from positive to negative, to do this we need to:
•Write the number is its equivalent positive binary form •Add 0's to the number to make it 8 bit •Invert each bit, changing 0's to 1's and 1's to 0's •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
There is one other method for representing numbers in two's complement form, without using calculations. To do this we need to:
•Write the number is its equivalent positive binary form •Add 0's to the number to make it 8 bit •Starting from the right and going left find the first 1 and keep it •Invert each bit, 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
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.
-128+64+16+2= -46