[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


What is negative 32768 for 16-bit ints?

Posted on 2004-10-08
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
LVL 16

Accepted Solution

PaulCaswell earned 400 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.

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


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 46

Expert Comment

by:Kent Olsen
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 46

Expert Comment

by:Kent Olsen
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use conditional statements in the C programming language.
Suggested Courses

656 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