Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where did the leading 1 means negative number in signed int arise from?

Even though I read a number of articles that say that mostly 2's complement is used to represent the negative numbers in a signed integer and that that is the best method,

However for some reason I have this (below) stuck in my head and can't get rid of it without knowing the history of it

"Use the leading bit as 1 to denote negative numbers when using signed int."

I have read many posts online & in StakOverflow that 2's complement is the best way to represent negative numbers. But my question is not about the best way, it is about the history or from where did the "leading bit" concept arise and then disappear?

P.S: Also it is just not me, a bunch of other folks were also getting confused with this.

Edit - 1 The so called leading 1 method I mentioned is described with an example in this post: Why is two's complement used to represent negative numbers?

Now I understand, the MSB of 1 signifies negative numbers. This is by nature of 2's complement and not any special scheme.

Eg. If not for the 1st bit, we can't say if 1011 represents -5 or +11.

Thanks to: jamesdlin, Oli Charlesworth, Mr Lister for asking imploring questions to make me realize the correct answer.

Rant: I think there are a bunch of groups/folks who have been taught or been made to think (incorrectly) that 1011 evaluates to -3. 1 denoting - and 011 denoting 3.

The folks who ask "what my question was.. " were probably taught the correct 2's complement way from the first instance they learnt it and weren't exposed to these wrong answers.

like image 360
Kingkong Jnr Avatar asked May 03 '12 06:05

Kingkong Jnr


People also ask

Can a leading one be negative?

What can we say about this number? It's first (leftmost) bit is 1, which means that this represents a number that is negative. That's just the way that things are in two's complement: a leading 1 means the number is negative, a leading 0 means the number is 0 or positive.

What is signed binary numbers and how do you represent the negative numbers?

The representation of a signed binary number is commonly referred to as the sign-magnitude notation and if the sign bit is “0”, the number is positive. If the sign bit is “1”, then the number is negative. When dealing with binary arithmetic operations, it is more convenient to use the complement of the negative number.

How is a negative integer stored in int datatype?

In most implementations that you are likely to encounter, negative signed integers are stored in what is called two's complement. The other major way of storing negative signed numbers is called one's complement. The one's complement of an N-bit number x is defined as x with all its bits flipped, basically.


1 Answers

There are several advantages to the two's-complement representation for signed integers.

Let's assume 16 bits for now.

Non-negative numbers in the range 0 to 32,767 have the same representation in both signed and unsigned types. (Two's-complement shares this feature with ones'-complement and sign-and-magnitude.)

Two's-complement is easy to implement in hardware. For many operations, you can use the same instructions for signed and unsigned arithmetic (if you don't mind ignoring overflow). For example, -1 is represented as 1111 1111 1111 1111, and +1 as 0000 0000 0000 0001. If you add them, ignoring the fact that the high-order bit is a sign bit, the mathematical result is 1 0000 0000 0000 0000; dropping all but the low-order 16 bits, gives you 0000 0000 0000 0000, which is the correct signed result. Interpreting the same operation as unsigned, you're adding 65535 + 1, and getting 0, which is the correct unsigned result (with wraparound modulo 65536).

You can think of the leading bit, not as a "sign bit", but as just another value bit. In an unsigned binary representation, each bit represents 0 or 1 multiplied by the place value, and the total value is the sum of those products. The lowest bit's place value is 1, the next lower bit is 2, then 4, etc. In a 16-bit unsigned representation, the high-order bit's place value is 32768. In a 16-bit signed two's-complement representation, the high-order bit's place value is -32768. Try a few examples, and you'll see that everything adds up nicely.

See Wikipedia for more information.

like image 160
Keith Thompson Avatar answered Oct 12 '22 20:10

Keith Thompson