We can represent a negative number in binary by making the most significant bit (MSB) a sign bit, which will tell us whether the number is positive or negative. The column headings for an 8 bit number will look like this:
-128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
MSB | LSB | ||||||
1 | 0 | 1 | 1 | 1 | 1 | 0 | 1 |
Here, the most significant bit is negative, and the other bits are positive. You start with -128, and add the other bits as normal. The example above is -67 in denary because: (-128 + 32 + 16 + 8 + 4 + 1 = -67)
-1 in binary is 11111111.
Note that you only use the most significant bit as a sign bit if the number is specified as signed. If the number is unsigned, then the msb is positive regardless of whether it is a one or not.
Signed binary numbers
If the MSB is 0 then the number is positive, if 1 then the number is negative.
0000 0101 (positive) 1111 1011 (negative) |
Converting Negative Numbers
To find out the value of a twos complement number we must first make note of its sign bit (the most significant, left most bit), if the bit is a zero we work out the number as usual, if it's a one we are dealing with a negative number and need to find out its value.
Method 1: converting twos complement to denary
To find the value of the negative number we must find and keep the right most 1 and all bits to its right, and then flip everything to its left. Here is an example:
1111 1011 note the number is negative 1111 1011 find the right most one 1111 1011 0000 0101 flip all the bits to its left
We can now work out the value of this new number which is:
128 64 32 16 8 4 2 1 0 0 0 0 0 1 0 1 4 + 1 = −5 (remember the sign you worked out earlier!) |
Method 2: converting twos complement to denary
To find the value of the negative number we must take the MSB and apply a negative value to it. Then we can add all the heading values together
1111 1011 note the number is negative -128 64 32 16 8 4 2 1 1 1 1 1 1 0 1 1 -128 +64 +32 +16 +8 +2 +1 = -5 |
How about a more complex example?
Method 1: converting twos complement to denary
1111 1100 note the number is negative 1111 1100 find the right most one 1111 1100 0000 0100 flip all the bits to its left 128 64 32 16 8 4 2 1 0 0 0 0 0 1 0 0 4 = −4 (remember the sign you worked out earlier!) |
Method 2: converting twos complement to denary
To find the value of the negative number we must take the MSB and apply a negative value to it. Then we can add all the heading values together
1111 1100 note the number is negative -128 64 32 16 8 4 2 1 1 1 1 1 1 1 0 0 -128 +64 +32 +16 +8 +4 = -4 |
So we know how to work out the value of a negative number that has been given to us. How do we go about working out the negative version of a positive number? Like this, that's how...
Method 1: converting twos complement to binary
Take the binary version of the positive number
0000 0101 (5) 0000 0101 find the right most one 0000 0101 1111 1011 flip all the bits to its left
So now we can see the difference between a positive and a negative number
0000 0101 (5) 1111 1011 (−5) |
Method 2: converting twos complement to binary
Take the binary version of the positive number
starting with -128, we know the MSB is worth -128. We need to work back from this:
-128 64 32 16 8 4 2 1 1 1 1 1 1 0 1 0 -128 +64 +32 +16 +8 +1 = -5 0000 0101 (5) 1111 1011 (−5) |
Convert the following two's complement numbers into denary: 0001 1011 Answer : (positive number) 27 1111 1111 Answer : (negative number) 0000 0001 = -1 0111 1101 Answer : (positive number) 125 1001 1001 Answer : (negative number) 0110 0111 = -103 1011 1000 Answer : (negative number) 0100 1000 = -72 81 (hexadecimal) Answer : (using 4 bits for each HEX char) 1000 0001 (negative number) -> 0111 1111 = -127 A8 (hexadecimal) Answer : (using 4 bits for each HEX char) 1010 1000 (negative number) -> 0101 1000 = -88 Convert the following numbers into negative numbers written in binary 0000 0001 Answer : 1111 1111 0110 0000 Answer : 1010 0000 0111 1111 Answer : 1000 0001 12 (denary) Answer : 0000 1100 = +12 -> 1111 0100 = -12 67 (denary) Answer : 0100 0011 = +67 -> 1011 1101 = -67 34 Answer : 0010 0010 = +34 -> 1110 1110 = -34 34 (hexadecimal) Answer : (using 4 bits for each HEX char) 0011 0100 = +52 -> 1100 1100 = -54 7E (hexadecimal) Answer : (using 4 bits for each HEX char) 0111 1110 = +126 -> 1000 0010 = -126
No comments:
Post a Comment