Can someone explain to me what this is and how its used and how it might be useful? The programming book only touched on it and the books at school are useless. 
Can someone explain to me what this is and how its used and how it might be useful? The programming book only touched on it and the books at school are useless.![]()
That said, unless you are programming at a fairly low level (that is with little abstraction between you and the hardware) you are unlikely to have to deal with it directly very often.
Andrew
int Ctr;
unsigned int Ctr;
If you program in "C" or "C++" or many other languages, you deal with it very often.
"int" as in :is 2's complement.Code:int Ctr;
I'm sure cqexbesd meant in the sense of directly twiddling the bits and doing an addition.
Can someone explain to me what this is and how its used and how it might be useful? The programming book only touched on it and the books at school are useless.![]()
Compliment = Saying something nice about someone.
Complement = Adding to something to make it complete.
1. You use binary digits to represent unsigned integer numbers. The least valued bit has a value of 1 (or 0), the second bit has a value of 2 (or 0), next bit has a value of 4, then 8, 16, 32, 64 etc. With 32 binary digits you can represent every unsigned integer number from 0 to 2^32 - 1 (two to the power of 32, minus 1).
2. To represent signed numbers as well, one bit is used to represent the sign of the number. For example, a 32 bit signed integer would use one bit as the sign bit, and the other 31 bits for the value. A positive number, like +5, is stored as sign bit = 0, and the remaining 31 bits contain the unsigned number 5. A negative number, like -5, is stored as sign bit = 1, and then there are different methods to decide what you would store in the other 31 bits.
3. A simple method is "signed magnitude" representation. In signed magnitude representation, the signed number -5 is stored as sign bit = 1, value bits = same as the unsigned number 5. Something similar is usually used for floating-point numbers. Signed-magnitude has the disadvantage that the two simplest operations, adding and subtracting, are quite complicated. You can see this by writing the bits for +5 and -5, and checking what happens to these bits if you add +1: Both numbers change in completely different ways.
4. Another method is called "one's complement" representation. To store the number -5, the sign bit is set to 1, the other bits are stored as the number 5, but with all bits reversed ("complemented"), so instead of 5 = 0000....0101, you store 1111....1010. This method has been used in some computers, like the Control Data supercomputers in the 1970's. Haven't seen it for a while.
5. A third, and the most common method, is called "two's complement". The simplest way to explain it is this: A positive number from 0 to 2^31 - 1 is stored in 32 bits exactly the same as if it was an unsigned number. A negative number x from -2^31 to -1 is stored exactly the same as the unsigned number 2^32 + x would be stored.
The biggest advantage of two's complement is that addition and subtraction of signed numbers can be performed using exactly the same hardware that is used for unsigned numbers. It's not just saving by using the same hardware, it is also the simplest possible hardware for these operations, and they are the most time critical operations in the processor of all. More complicated add/subtract would mean that your processor clock speed would go down.
Compliment = Saying something nice about someone.
Complement = Adding to something to make it complete.