Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Let suppose I have a integer. I want to check whether that number is unsigned. How to put if statement
Posted
Comments
Philippe Mori 4-Sep-11 21:29pm    
Where do you have an integer? Do you have a value in code like 25 (hard-coded constant). A variable that contain a value. A string that you read?

You cannot do that. Signed and unsigned are just different interpretations of the same bit pattern, for instance, on a 32 bit machine, the bit pattern 0xFFFFFFFF is -1 if interpreted as signed while is 4,294,967,295 if interpreted as unsigned.
If you know the size of the variable, however, then you may check the most significant bit, if it is 0 then the variable represents the same number both in the signed and unsigned interpretation (e.g. 0x00000001 is 1 both in signed and unsigned interpretation).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Sep-11 0:40am    
I voted 5, but must add that you should have explained that signed or unsigned is still a valid attribute, but of a type, not of its instance (value). You correctly explained why the value is not signed or unsigned.
--SA
CPallini 5-Sep-11 3:13am    
Thank you.
Yeah, I missed to explain that an unsigned int is an unsigned while a int is a signed (just kidding!).
Stefan_Lang 5-Sep-11 8:36am    
Good explanation, but looking at the question I truly wonder if that is what the poster wanted to know. I can see at least 3 valid interpretations of the question above (including the one addressed in S3), but I might need to build a planet-sized computer and have a depressive robot wait in an underground garage for a few million years while it elaborates the question you produced the answer to...

;-p
1. If you want to know if an integer value is negative (not 'signed' - 'signed' is a property of a type, not a property of a value!), then just write
if (mynumber < 0) {
   puts ("this value is negative");
}


2. If you want to make sure that a particular type is signed (or unsigned), then use the appropriate qualifier in the declaration:
C++
char mychar; // wrong: is it signed or unsigned?
unsigned char myunsigned; // ok, this type is unsigned
signed char mysigned; // ok, this type is signed


3. If you want to check an integral template type argument to find out whether it is a signed or unsigned type, see solution 3

4. If you want none of the above, please be more concise: please specify in the terminology of C/C++ what you mean by the terms 'integer' or 'number'; please verify that you were using the correct term when you said 'unsigned'.
 
Share this answer
 
Comments
Philippe Mori 5-Sep-11 9:11am    
My 5. Very good answer.
Boost library has a type trait just for that:

is_unsigned[^]

By the way, I think your question was badly formulated and Solution 2 (now removed) was probably the answer to your "real" question is you want to know if a number like 18 is unsigned...
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 5-Sep-11 0:43am    
Good link, but as to the second statement... No, your solution is not to the "real" question, simply because such question could not be answered. See the solution by C Pallini. I voted 4, but overall, with my comments, I think we finally explained that matter all together.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900