You are not logged in.

#1 Yesterday 08:39:54

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 371
Website

C unsigned integrals wrapping behaviour.

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

#2 Yesterday 15:13:07

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 659

Re: C unsigned integrals wrapping behaviour.

uint32_t wrap;
wrap = -50;
wrap = LONG_MAX;

This is implicit signed to unsigned conversion: https://en.cppreference.com/c/language/ … onversions

Offline

#3 Today 02:23:38

mpan
Member
Registered: 2012-08-01
Posts: 1,609
Website

Re: C unsigned integrals wrapping behaviour.

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

#4 Today 08:54:09

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 371
Website

Re: C unsigned integrals wrapping behaviour.

Alright, thanks for your inputs.

After thinking about it, it's most probably platform dependent.

Online

#5 Today 12:02:25

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 659

Re: C unsigned integrals wrapping behaviour.

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.

Offline

#6 Today 12:24:19

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 371
Website

Re: C unsigned integrals wrapping behaviour.

dimich wrote:
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

#7 Today 12:42:05

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 659

Re: C unsigned integrals wrapping behaviour.

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?

Offline

#8 Today 12:46:23

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 371
Website

Re: C unsigned integrals wrapping behaviour.

dimich wrote:
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

#9 Today 13:06:47

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 659

Re: C unsigned integrals wrapping behaviour.

ReDress wrote:

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.

ReDress wrote:

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

#10 Today 14:29:33

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 371
Website

Re: C unsigned integrals wrapping behaviour.

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

#11 Today 15:08:59

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 659

Re: C unsigned integrals wrapping behaviour.

ReDress wrote:

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.

ReDress wrote:

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

#12 Today 15:34:31

mpan
Member
Registered: 2012-08-01
Posts: 1,609
Website

Re: C unsigned integrals wrapping behaviour.

ReDress wrote:

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.

ReDress wrote:

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

#13 Today 16:21:54

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 371
Website

Re: C unsigned integrals wrapping behaviour.

Just now you prove even further that you're not even in the slightest, knowledgeable about the issue.

My usual scapegoat :- the compiler!

Online

#14 Today 16:34:03

Whoracle
Member
Registered: 2010-11-02
Posts: 223

Re: C unsigned integrals wrapping behaviour.

Snide remark redacted. I'll rather report this.

Last edited by Whoracle (Today 16:35:34)

Offline

#15 Today 16:36:54

5hridhyan
Member
Registered: 2025-12-25
Posts: 916
Website

Re: C unsigned integrals wrapping behaviour.

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

#16 Today 16:38:13

cryptearth
Member
Registered: 2024-02-03
Posts: 2,212

Re: C unsigned integrals wrapping behaviour.

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

#17 Today 16:40:22

5hridhyan
Member
Registered: 2025-12-25
Posts: 916
Website

Re: C unsigned integrals wrapping behaviour.

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

#18 Today 17:17:51

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,430

Re: C unsigned integrals wrapping behaviour.

@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

#19 Today 17:20:38

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 371
Website

Re: C unsigned integrals wrapping behaviour.

@Seth No, thanks.

This is related to my very classified research interests which I am not willing to disclose at any cost.

Online

#20 Today 17:29:07

5hridhyan
Member
Registered: 2025-12-25
Posts: 916
Website

Re: C unsigned integrals wrapping behaviour.

<*>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

Board footer

Powered by FluxBB