code check! is my code correct?


bool check(int test)
{
if(test & 0x80000000)
return 1;//true signed value
else return 0;//not signed value

}

Would the above code give me correct results?
It is not possible to determine if a variable is signed or unsigned.
Last edited on
closed account (S6k9GNh0)
I don't really understand how any of that works. A variables sign isn't handled at runtime and is instead handled at compile-time through the typing system. Even if you can determine at runtime (which would be purely based on how the compiler treats each sign differently) it would be inefficient.

Boost.TypeTraits contains a mechanism to determine this at compile-time: http://www.boost.org/doc/libs/1_49_0/libs/type_traits/doc/html/boost_typetraits/reference/is_unsigned.html

EDIT: I love D when it comes to situations like these.
EDIT2: If you don't want to use Boost, an implementation of those mechanisms can easily be hand-made, I can explain how.
Last edited on
The test above is checking to see if an int is negative, not signed (assuming that the "int" type is a 32-bit 1s-complement or 2s-complement number). Of course, testing "(test < 0)" is a safer and probably smaller and/or faster way of testing for negative values.

(By definition, all "int" values are signed in C/C++).
Last edited on
Topic archived. No new replies allowed.