Solved

What is negative 32768 for 16-bit ints?

Posted on 2004-10-08
10
1,227 Views
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?

Thanks
stevefromc

0
Comment
Question by:Stephen Kairys
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Accepted Solution

by:
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.

Paul

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

0
 
LVL 4

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 :)  ?

Thanks
0
 
LVL 16

Expert Comment

by:PaulCaswell
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.

Paul
0
 
LVL 4

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.

stevefromc
0
 
LVL 12

Expert Comment

by:stefan73
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.

Cheers!

Stefan
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

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

Expert Comment

by:Kdo
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.


Kent
0
 
LVL 16

Expert Comment

by:PaulCaswell
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.

Paul
0
 
LVL 4

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 :)
0
 
LVL 45

Expert Comment

by:Kdo
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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 how to use strings and some functions related to them in the C programming language.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now