Question

I know that with a signed integer there is a sign bit that is usually located at the leftmost bit that has a 0 or a 1 to show whether the integer is a negative or positive number.

I tried looking everywhere but was not able to find the answer to my question.

Is there any advantage of having the leftmost bit the sign bit as opposed to the rightmost or any other bit? Or is it essentially the same thing?

Was it helpful?

Solution

Careful, most computer integers do not actually use a sign bit per se, but rather use what is called 2's complement to store numbers in a signed fashion. It is true, however that 2's complement will cause the "left-most", or more specifically, the "most significant" bit to be set to 1 when a number is negative.

Long story short, by using the 2's complement method to encode integer values, the math processing unit on a processor can use some cool tricks to speed up processing of addition and subtraction operations. For example, adding a negative 2's complement number to a positive 2's complement number can be done using the same hardware logic as standard addition, whereas a sign bit would not allow this.

Another system for representing signed integers, but not used as often (citation needed) is 1's compliment. It is simpler to directly understand a number (for us humans) in 1's compliment because to store a negative number in 1's compliment, you just flip all of the bits. Notice that there still is not technically a sign bit, even though the most significant bit will always be 1 when the number is negative or 0 when non-negative (except in the case explained below). The advantage here is once again simplified hardware logic for arithmetic operations on the numbers compared to using just a straight sign bit.

1's compliment does suffer from one of the problems that a number using a sign-bit suffers from, and that is that it can store the number 0 in two ways. For an 8-bit example, 0 in 1's compliment can be stored both as 1111 1111 or as 0000 0000. Essentially, this means you can store 0 both as a positive and negative value, which any mathematician will tell you makes no sense. Essentially, 0 does not have a sign, positive or negative. It is just 0. A computer scientist will look at the mathematician and say "so what," and that's because it just means we need to handle a special case in our logic for the possibility of two ways of storing 0. Luckily 0 is the only number that has this problem, but do we really want to always have to check if our result is one of two possible answers? Even if this is done at the hardware level and hidden from the programmer (which would slow down the calculation, even if just marginally)?

As I mentioned above, using a sign bit suffers from the same positive/negative 0 issue that 1's compliment suffers from, and it requires a much larger set of special cases in the hardware logic that make using this storage method both confusing (in the case of 0) and expensive (in terms of time and physical hardware logic) to use for storing signed numbers.

All of that being said, there is still the question of "why the leftmost bit?" Ultimately, it is a question for philosophers. Ultimately, there is no distinct advantage at the hardware level for using the most significant bit. Any of the hardware logic required to manage numbers is not complicated by the location of the bit. As far as the hardware is concerned, the bit could be in the middle. In fact, the hardware circuitry could actually be laid out physically with the most significant bit in the middle, but logically it is at the left. But ask yourself, does it make sense? Here in America, we typically write negative numbers a la -244521, with the negative sign at the left. So why not put the sign bit at the left?

In fact, this debate exists at a higher level when talking about how to store numbers larger than 8 bits in memory. You can find countless discussions about the Endianness of numbers, and which one is better. In the end, it just depends on what you are trying to accomplish, but that's outside the scope of your question.

Philosophy aside, as stated in this answer, placing the sign bit in the leftmost bit does provide a natural alignment between numbers that are signed and unsigned, which is usually a good thing for our slow human brains.

OTHER TIPS

The advantage is that for signed int and unsigned int the alignment of bits/values remain the same.

Signed int with value 10 is equal to unsigned int value 10. Because of same alignment. If you would put the sign-bit to the right those values would be different .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top