You are not logged in.

#1 Yesterday 08:39:54

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 368
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;
}

Offline

#2 Yesterday 15:13:07

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

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

Online

#3 Today 02:23:38

mpan
Member
Registered: 2012-08-01
Posts: 1,608
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: 368
Website

Re: C unsigned integrals wrapping behaviour.

Alright, thanks for your inputs.

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

Offline

#5 Today 12:02:25

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

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.

Online

#6 Today 12:24:19

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 368
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.

Offline

#7 Today 12:42:05

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

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?

Online

#8 Today 12:46:23

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 368
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.

Offline

#9 Today 13:06:47

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

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.

Online

#10 Today 14:29:33

ReDress
Member
From: Nairobi
Registered: 2024-11-30
Posts: 368
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.

Offline

Board footer

Powered by FluxBB