Binary 32767 & 32768

I was thinking about ints and shorts now in C#.  Shorts in C# can hold values from -32768 to 32767.  I know that other languages do this the other way round -32767 to 32768.  I was wondering why either the negative or positive has one extra digit.  I did study and understand this but it was 20 years ago and I have forgot and can't figure it out.  If we have 16 bits and one of them is used to indicate whether the number is negative or positive then the remaining bits can therefore hold values from 0 to 32767.

I knocked a little spreadsheet together to illustrate this and tinker with but I can't figure out why either negative or positive numbers have one extra digit.  Clearly there is a good reason - it just escapes me at the moment.  If you want to look at my spread sheet you can get it at:


LVL 19
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hello Dave,

Lets simplify this by looking at TinyInts instead of ints and shorts.
The Byte type takes numbers from 0 to 255. But lets use the same byte to represent both positive and negatives
A byte can hold a total of 256 numbers. This includes the number zero.
Hence there are 255 left. Either 128 positive and 127 negative, or 128 negative and 127 positive.

A similar explanation goes for shorts and ints



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
daveamourAuthor Commented:
Ahh I get it now

If say we use the following for the sign indicator bit

1 = positive
0 = negative

Then we can have 2 zeros:


ie positiver zero and negative zero - nonsense of course so one of the redundant zeros is used to increase either the positive or negative range by one - a choice of the language developers hence why I remember it being -32767 to 32768 but in C# it is the other way round!

I just ran into this and I wanna point out that the solution accepted is false.

The reasoning is as follows.
In signed binary, an integer goes from  32767 to - 32768 because it has 2 bytes assigned in memory.
each byte is 8 bits so it has a total of 16 bits in memory. Imagine each bit as a box that fits one entry.
In binary the first box from the left is assigned for + or - for signed integers like the one your asking about.
Which leaves only 15 boxes to work with for the numbers.
+32767 in binary is:
0111111111111111  (0 and 15 ones)
-32768 in binary is:
1000000000000000  (1 and 15 zeros)

In signed integers, the leftmost bit is used to indicate the sign. if the first of the 16 bits is a zero then it's positive. If the sign bit is set (=1), then the number is negative. Since one bit is used for the sign, then there are only 15 bits to work with.
1111111111111111 (16 ones)   is actually -1 in decimal, hint they start backwards and they can end with (one and 15 zeros) while the positive has to end with (zero and 15 ones) to keep the first bit a zero.

hope this makes a little more sense.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.