Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
Question :
 
#include <stdio.h>
int main( ) {
       signed int num = 0xDEADBEE;
}
What is the sign of the variable num ?
Posted 15-Jan-13 20:32pm
Edited 15-Jan-13 21:11pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Depends.
 
If you are running in a system where int is a 16 bit value, then you will either get an overflow compilation error, or a negative value.
In a 32 bit integer system (or higher) the value would be positive.
 
It's all about the most significant bit of signed integers - if it is zero, it's positive, if one it's negative. Since your value has only 28 bit positions defined out if a possible 32, the remaining 4 bits will be zero, and the result is a positive number.
 
Most modern compilers (except for embedded work) are 32 bit, with occasional 64 or higher for specialist applications. Embedded compilers (or cross compilers) may well be 16 bit.
 
"Dear sir, If the signed num is 0x16 then its binary form is 0001 0110 the msb is 0 --> positive.
for the negative signed num,how to find and locate that msb? eg : 0xA and 0xABDE for both scenarios is same or not?"

 
Not quite!
If the value is 0x16 then it's binary form is not
                              0001 0110
it is
                    0000 0000 0001 0110
for a sixteen bit system, and
0000 0000 0000 0000 0000 0000 0001 0110
for a thirty-two bit system.
The position of the MSB changes according to the size of the variable it is going into. So you need to know what size data you are working with in order to decide if a given value is positive or negative.
(This is one advantage of working with C# and similar .NET langauges: int is defined by the language specification as a synonym for int32 - a thirty-two bit signed integer.)
 
For a sixteen bit system, "just the MSB" is 0x8000, for a thirty-two bit system it is 0x80000000
Binary ANDing with this value will tell you if it is positive or negative.
  Permalink  
v2
Comments
mvigsnhwar at 16-Jan-13 4:25am
   
Dear sir, If the signed num is 0x16 then its binary form is 0001 0110 the msb is 0 --> positive.
for the negative signed num,how to find and locate that msb? eg : 0xA and 0xABDE for both scenarios is same or not?
OriginalGriff at 16-Jan-13 4:47am
   
Answer updated
SajeeshCheviry at 16-Jan-13 5:03am
   
good answer :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 389
1 Nirav Prabtani 268
2 Richard Deeming 250
3 _Amy 185
4 CHill60 170
0 OriginalGriff 8,104
1 Sergey Alexandrovich Kryukov 7,067
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100