Reputation: 13
Following code:
UINT32 dword = 4294967295;
if(dword + 1 != 0) // condition
In such operations is there any warranty that the biggest (whole) register (avaiable on architecture) is always used? and above condition will be always true on 64 bit mode, while false for 32 bit mode?
Upvotes: 1
Views: 122
Reputation: 490008
That'll depend on what sort of type UINT32
really is.
If it's an unsigned type (as you'd expect) then results are guaranteed to be reduced modulo the largest value that can be represented + 1, so code like this:
if (std::numeric_limits<T>::is_unsigned)
assert(std::numeric_limits<T>::max()+1==0);
...should succeed. OTOH, based on the name, we'd typically expect that to be a 32-bit type regardless of the implementation, register size, etc., so we'd expect to get the same result regardless.
Edit: [sorry, had to stop and feed baby for a few minutes] I should add in more detail. Although we can certainly hope it's unlikely in practice, it's conceivable that UINT32
could really be (say) a 16-bit unsigned short
. For the sake of discussion, let's assume that int
is 32 bits.
In this case, dword+1
would involve math between an unsigned short
and an int
(the implicit type of 1
). In that case, dword
would actually be initialized to 65535. Then, when you did the addition, that 65535
would be promoted to a 32-bit int
, and 1
added as an int
, so the result would be 65536.
At least in theory, the same basic thing could happen if UINT32 was an unsigned 32-bit type (as we'd expect) but int
was a 64-bit type. Again, dword
would be promoted to int
before doing the math, so the math would be done on 64-bit quantities rather than 32-bit, so (again) the result would not wrap around to 0.
Upvotes: 3