Do sign bits count in the given length of a processor register? Like, does an 8 bit register consist of 8 bits plus a sign bit, or 7 bits plus a sign bit? I was guessing it was part of the overall number and then the register rolls over when the number is large enough.
? The compiler only tracks what you tell it. Whether a variable is signed or not is based on what you set it to be. In other words, there is no bit that determines sign, it's handled in compile-time type data.
From what I understand, Two's Complement is simply a method of turning the same binary data into a negative number. For instance:
1111 1111 is 255 in decimal as an unsigned value.
1111 1111 is -1 in decimal as a signed value using two's complement.
Using math from wikipedia, you find two's complement by taking 2^N where N is the size of binary value and subtracting that from current value. For instance, for the above example and when working with a byte value, take 2^8 which is 256 and subtract 256 from 255 which gives you -1.
A neat trick you can do to set an unsigned integer to maximum value is set it to -1. http://codepad.org/SNCL2jHr
x86 processors use a different set of instructions for operating on signed data vs operating on unsigned data. So if your code uses the signed keyword (in C++), the compiler, when operating on your signed data, will produce instructions meant for signed information.
unsigned 8-bit values can store values from 0-255 and does use the first bit, but signed 8-bit values can store values from -128 - +127. If the first bit (the sign bit) is 1, the value is negative, if it is 0, the value is positive. Unsigned data doesn't check the sign bit (the value isn't negative if it is 1 or positive if it is 0, but rather, interprets the entire 8 bits as one number).