Saturday, May 22, 2010

About unsigned integers (c++)?

e.g.


unigned int uint;


uint=0x3874;





Then I say c++ to 'cout' uint and the result is a long integer, approximately 40438 or something like that. how!!!???

About unsigned integers (c++)?
You're setting uint to decimal 14452 (hex 3874) which is OK even for a 16 bit integer. Your problem must be somewhere else.
Reply:40000 + isn't necessarily a long int type only value. Standards for this are all hardware dependent to some degree in the larger ranges. You aren't seeing the forest for the trees here, though.





I hope this will explain what is happening.





The value is out of range for a signed integer simply because the signed integer's range of valid values starts in negative territory. When you assign it to an unsigned integer, which has the range start at 0, then there can be twice the positive value stored in the same integer sized memory.


No comments:

Post a Comment