You are not logged in.
I am confused about the wrapping behaviour of unsigned integrals in C.
It's does wrap predictably upto a certain point but then beyond that point, the behaviour becomes more or less undefined.
Could someone possibly clarify how exactly this works?
#include <stdint.h>
int main(int argc, char *argv[])
{
uint32_t wrap;
wrap = -50;
wrap = LONG_MAX;
return 0;
}Offline
uint32_t wrap;
wrap = -50;
wrap = LONG_MAX;This is implicit signed to unsigned conversion: https://en.cppreference.com/c/language/ … onversions
Online
ReDress: it going to be much easier to explain, if you tell us where your misunderstanding occurs. You did show code that is a no-op.
A very simple principle:
What do you expect to see?
What do you actually see? Don’t skip this point. I repeat: don’t skip this point.
Why do you think you should see the expected result?
Since the above code is also platform-dependent, tell us which platform and compiler that is.
Last edited by mpan (Today 02:24:20)
Offline
Alright, thanks for your inputs.
After thinking about it, it's most probably platform dependent.
Offline
it's most probably platform dependent.
There is no wrapping in your example.
Imagining the code is not no-op, value of wrap after wrap = -50; is well defined and platform-independent.
Result of wrap = LONG_MAX; is implementation-defined, depending on whether long is exactly 32 bit or wider than 32 bit on your target.
Online
ReDress wrote:it's most probably platform dependent.
There is no wrapping in your example.
Imagining the code is not no-op, value of wrap after wrap = -50; is well defined and platform-independent.
Result of wrap = LONG_MAX; is implementation-defined, depending on whether long is exactly 32 bit or wider than 32 bit on your target.
Well, the link you shared illustrates that it's well defined in the C language but don't forget it's possible to bypass the compiler and write in assembly. Unless I am terribly mistaken, that's possible.
Offline
it's possible to bypass the compiler and write in assembly. Unless I am terribly mistaken, that's possible.
Of course, assembly is platform-dependent and implementation-specific. However, on the vast majority of platforms signed integers are represented as two's complement. But is has nothing to do with C. What is your actual question?
Online
ReDress wrote:it's possible to bypass the compiler and write in assembly. Unless I am terribly mistaken, that's possible.
Of course, assembly is platform-dependent and implementation-specific. However, on the vast majority of platforms signed integers are represented as two's complement. But is has nothing to do with C. What is your actual question?
Okay, let me rephrase the question in the manner you want.
Is the behaviour observed when implicitly converting to unsigned int well defined and predictable? Also, please don't answer this question if this is not something you have personally observed.
Offline
Is the behaviour observed when implicitly converting to unsigned int well defined and predictable?
In C? Yes, it is.
uint32_t wrap = -50; is well defined and predictable.
uint32_t wrap = LONG_MAX; is well defined and predictable within particular platform. On different platforms you may get different results.
Also, please don't answer this question if this is not something you have personally observed.
Could you please clarify what observation is [un]expected? I develop in C for more than two decades and never seen implicit conversion don't obey the standard.
Online