What is negative 32768 for 16-bit ints?

Posted on 2004-10-08
Last Modified: 2012-05-05

Just curious.
When testing a routine, I encountered the following that relates to this code:

int foo; // here int is 16-bits, of ranger -32768 to +32767

foo = =32768;
foo *= -1;
printf("foo is now %d\n", foo);

here, foo still is -32768: the value has not changed.
Now, I know you cannot have +32768 in the 16-bit world, but I'm trying to understand
what's happening here. When you multiply by -1, you are changing the SIGN BIT,
so how can the value remain the same?


Question by:Stephen Kairys
  • 4
  • 3
  • 2
  • +1
LVL 16

Accepted Solution

PaulCaswell earned 100 total points
ID: 12259914
There are always two special points in the signed arithmetic number scale on computers. These are 0 and the same number with the highest bit set.

In the case of 0, negation obviously makes no sense so, to achiev negation, we dont just set the sign bit, we use 'compliment increment'. This essentially negatest each bit and then adds one to the final result. In 8 bits, for easy demonstration.

0 = 00000000
Invert gives you 11111111
and increment gives 00000000 as desired.

This effect also happens at the other end as a side-issue.

128 = 10000000
Invert gives 01111111
add 1 gives 10000000 as before.

In 16bit, 32768 is the equivalent of 128 in 8bit.


P.S. Historically, the 'NEG' mnemonic for the negate assembler instruction was 'CIA' on the old 6502 assembler, standing for 'Compliment Increment Accumulator'.


Author Comment

by:Stephen Kairys
ID: 12259987
OK, sounds good, but I just tried it for the number 1,

1 = 00000001
invert gives 11111110
add 1 gives 11111111

which is actually -1, right? If this var. were UNSIGNED it would be 255 (the highest possible
value of an 8-bit), so if it were signed it must be -1 (the first value "below" 0).

Did I get it :)  ?

LVL 16

Expert Comment

ID: 12260044
Spot on!!!!

The crucial point is that this behaviour happens across the whole spectrum of possible numbers except at each end just because -0 must be the same as 0.

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Author Comment

by:Stephen Kairys
ID: 12260080
Negative 0. What a concept! Wonder if you can divide by it ;)

Seriously, though, thank you for an informative and interesting answer. Raising points
from 50 to 100.

LVL 12

Expert Comment

ID: 12260087
Hi PaulCaswell,
> -0 must be the same as 0
Now that's interesting. I never saw the '+1 bias' of neg that way.


LVL 16

Expert Comment

ID: 12260099
Mmmmmmm!!!!!!! Glad to be of service!!!!!!
LVL 45

Expert Comment

ID: 12287735

Hi StevefromC,

Actually, the concept of negative 0 has been around for a long time.  It's found in 1's complement architecture, where the negative value of any positive integer is obtained simply by toggling all of the bits in the word.

000000000000001 = 1
111111111111110 = -1

000000000000010 = 2
111111111111101 = -2

etc.  Hence,

000000000000000 = 0
111111111111111 = -0

When there were lots of vendors making room-sized computers, there was a huge argument among engineers as to which was better.  Both -0 and negative values that aren't bit complements of their positive values are abstractions so both sides had good arguments. To me, having a "negative 0" bit pattern that tests true for both negative (which tests only the sign bit) and zero (which tests that all bits are the same) makes more sense than the logical complement not equalling the arithmetic complement.

A couple of things to note.  When used as a operand, negative zero behaves exactly like positive zero.  Divide by it and generate a fault.  Add it and get the value of the other operand, etc...  The arithmetic units of those computers would not generate a negative 0.  You could only do that via the shift or boolean units.

LVL 16

Expert Comment

ID: 12288120
Thanks Kent, its good to feel that there is at least one other old codger out there although I have to admit I wasnt quite a programmer when the 'room-sized' guys were around. I have seen ferrite-ring 'core' in wooden frames in a working computer. It was about 'half-room' sized and it was a typesetter (an APS micro 5) about 15 years ago.


Author Comment

by:Stephen Kairys
ID: 12288391
Thanks for the info, Kent.  

Btw, and this sort of dates me, I wrote my first program back in 1974 on an IBM1130 in
8th grade, in BASIC. We fed in the interpreter through punch-cards, and typed in the program
w/o, as I recall the ability to save, and a rather small limit to the size of the program.

To say nothing of typing in my first FORTRAN program on punch-cards :)
LVL 45

Expert Comment

ID: 12289185

What a day.  I get called an old codger by Paul (who is due congratulations for making the top 10!) and am then reminded of my "seniority" by Stete.

Kids.....  :)

BTW, my first real experience with real computers was in college in 1974.

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get time_t value that is a week old 6 168
How do I install gcc 4.8.4 on a Linux Ubuntu 14.04 machine? 5 1,754
How to learn Linux? 10 61
outlook office 365 8 122
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question