Integers are represented internally as (typically) 32 bit 2's complement binary numbers. So, the number 56 is represented as
0000 0000 0000 0000 0000 0000 0011 1000
As with base 10, each digit has a "place" that determines it's value. On the far right is the 1's place. But the digit second to the left is the 2's place, then the 4's place, etc. If we number the digits from right to left as n=0 to n=31, we can figure out the value of a "place" as 2^n, where ^ is the exponent operator. So, in the example above, 56 is being represented as
0 * 2^0 = 0 +
0 * 2^1 = 0 +
0 * 2^2 = 0 +
1 * 2^3 = 8 +
1 * 2^4 = 16 +
1 * 2^5 = 32 +
0 * 2^6 = 0 +
:
= 8 + 16 + 32 = 56
That's basic binary. Now for negative numbers, we must use 2's complement notation. For an ordinary positive value like 56, we don't have to do anything. It's already in 2's complement. For a negative value, we first invert the bits and add 1. "Inverting bits" means that we change each 0 to a 1, and each 1 to a 0.
So, let's start with the binary for 123:
0000 0000 0000 0000 0000 0000 0111 1011
(this represents 64 + 32 + 16 + 8 + 2 + 1)
Now we invert all the bits:
1111 1111 1111 1111 1111 1111 1000 0100
Now, we add 1. In this example, this is very easy. We simply put a 1 in the far right position:
1111 1111 1111 1111 1111 1111 1000 0101
Okay, so this string of bits represents -123.
If you add the bits for 56 and -123 together,
0000 0000 0000 0000 0000 0000 0011 1000
+ 1111 1111 1111 1111 1111 1111 1000 0101
You get:
1111 1111 1111 1111 1111 1111 1011 1101
(I just added each corresponding bit. Happily, no pair of bits contained 2 1's, so this example was easy.)
So, how do I know that the result I just got is correct? Well, we're using 2's complement, and I can tell by all the 1's on the left that this is a negative number. This is good. To find out which negative number, I'll repeat the process I used for converting a positive number into negative 2's complement. (This is really a negation operation.) So once again, I'll invert the bits and add 1:
1111 1111 1111 1111 1111 1111 1011 1101
inverted:
0000 0000 0000 0000 0000 0000 0100 0010
add 1:
0000 0000 0000 0000 0000 0000 0100 0011
Now, decoding this number, we see that it is
1 + 2 + 64 = 67
So, the result represents the value -67, which is the correct answer.
2006-08-11 05:10:05
·
answer #1
·
answered by arbeit 4
·
0⤊
0⤋
Computers don't evaluate, programs do. In a word processor it would print out as "56-123". In a spreadsheet it would be the same. If enterred in a spreadsheet as =56-123 it would display as -67. You must be cleared as to what you want.
2006-08-11 12:06:34
·
answer #2
·
answered by Barkley Hound 7
·
0⤊
0⤋
Well is simple like that : 56 -123 = -67
Strange question .....anyway.....LOOL
If you wanna know more.....
Feel free to contact me on yahoo messenger for more details.
2006-08-11 11:39:44
·
answer #3
·
answered by PC Doctor 5
·
0⤊
0⤋