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;
}Online
uint32_t wrap;
wrap = -50;
wrap = LONG_MAX;This is implicit signed to unsigned conversion: https://en.cppreference.com/c/language/ … onversions
Offline
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.
Online
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.
Offline
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.
Online
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?
Offline
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.
Online
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.
Offline
Just so you know, I have been at this on an off for over two years.
Sometimes even the compiler explicitly rejects some values.
I am convinced you have no first hand experience with the issue and will disregard any of your further comments.
Online
Sometimes even the compiler explicitly rejects some values.
A compiler may warn you if value is changed during conversion, and fail to compile if configured to turn warnings into errors. But this has nothing to do with the C language and conversion rules.
It's impossible to answer a question that hasn't been asked. If you showed a real example, described the conditions, expected and observed behavior, a useful answer could be given.
I am convinced you have no first hand experience with the issue and will disregard any of your further comments.
It's up to you. It seems I'm feeding the troll, so it's a good time to stop.
Offline
After thinking about it, it's most probably platform dependent.
Whether it is or is not platform-dependent, can’t be said without you giving the answers to the list I posted above. I don’t think that you may make that judgement yourself, while at the same time asking to explain the concept.
One line in your code describes exactly the same operation on any platform that has `uint32_t` defined.
Is the behaviour observed when implicitly converting to unsigned int well defined and predictable?
Well-defined? Yes. Predictable — depends on what you mean by “predictable.” Again, if you’d answer the questions I asked, it would be easier to reply without all the pointless maybes and dependses.
Offline
Just now you prove even further that you're not even in the slightest, knowledgeable about the issue.
My usual scapegoat :- the compiler!
Online
Snide remark redacted. I'll rather report this.
Last edited by Whoracle (Today 16:35:34)
Offline
Thank god. Looks like I'm not the only one looking for a English to English translator.
Last edited by 5hridhyan (Today 16:39:56)
Online
why do even bothered to come back and "ask" a pointless "question" (not that this topics op does contain any proper one) if yo don't care about others replies but rather continue what you got evicted for? do you really seek that strongly for another eviction?
you've proven yourself to not really fit here - i recommend you dial back unless you want to risk a permanent good bye
Offline
If I paraphrase they were likely asking
How exactly does implicit conversion to an unsigned integer work in C when you give it a value outside its normal range?
is this wrapping behavior strictly defined and predictable, or does it eventually become undefined/platform-dependent?
Which I believe was answered... already...
Last edited by 5hridhyan (Today 16:42:28)
Online
@ReDress if you want an informed comment on a specific scenario, present that case. What you do, what you expect, what happens, what compiler you use.
Postulating some bold claim, presenting an unrelated, nonsensical (all of that probably just gets optimized away anyway) generic code and claiming everyone who is not aware of your still undisclosed problem is tantamount trolling, the response to which, rest assured, is very predictable and deterministic.
Online
@Seth No, thanks.
This is related to my very classified research interests which I am not willing to disclose at any cost.
Online
<*>No AKSUAL reproducible program
<*>No compiler/toolchain output
<*>No observable behavior shared
<*>No willingness to share details
<*>But still a strong claim that “smtg is wrong”
Nice.
Online