Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This behavior is more informally called unsigned integer wrapping. Unsigned integer operations can wrap if the resulting value cannot be represented by the underlying representation of the integer. The following table indicates which operators can result in wrapping:

Operator

Wrap

 

Operator

Wrap 

Operator

Wrap 

Operator

Wrap

+

Yes 

-=

Yes 

<<

Yes 

<

No

-

Yes 

*=

Yes 

>>

No

 

>

No

*

Yes 

/=

No 

&

No 

>=

No

/

No

 

%=

No 

|

No

 

<=

No

%

No 

<<=

Yes 

^

No 

==

No

++

Yes 

>>=

No 

~

No 

!=

No

--

Yes 

&=

No 

!

No

 

&&

No

=

No 

|=

No 

un +

No 

||

No

+=

Yes

 

^=

No 

un -

Yes

 

?:

No

Although unsigned left shift << can result in wrapping, modulo behavior is permitted by this standard because of common usage, because this behavior is usually expected by the programmer and because the behavior is well defined.

...

This compliant solution performs a postcondition test to ensure that the result of the unsigned addition operation to i is not less than the operand ui_a:

 

Code Block
bgColor#ccccff
langc
atomic_int i;
int ui_a;
 
/* Initialize ui_a, i */
 
atomic_fetch_add(&i, ui_a);
if (atomic_load(&i) < ui_a) {
  /* Handle error condition */
}

Exceptions

...