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.

Assuming you're talking about x86 (other platforms probably do it differently), read up on two's complement.

A signed 8bit number is: 7 bit of value, and a parity bit (you wrote sign bit, but yeah, that is its use). So it is memory-aligned.

I've never understood that. How can 8 bits store values up to 255 if they can't use the first bit? 255 has all 8 bits set, so how does the processor know whether it's signed or unsigned?

It doesn't, it just stores an arbitrary 8-bit number, and your program interprets its however you coded it to.

Then how can a signed 8 bit integer store 255 values in C? Does the compiler track it another way?

closed account (*S6k9GNh0*)

? 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

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

Last edited on

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).

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).

Topic archived. No new replies allowed.