c++ safeness of code with implicit conversion between signed and unsigned

francesco

According to the rules on implicit conversions between signed and unsigned integer types, discussed here and here, when summing an unsigned int with a int, the signed int is first converted to an unsigned int.

Consider, e.g., the following minimal program

#include <iostream>

int main()
{
   unsigned int n = 2;
   int x = -1;

   std::cout << n + x << std::endl;

   return 0;
}

The output of the program is, nevertheless, 1 as expected: x is converted first to an unsigned int, and the sum with n leads to an integer overflow, giving the "right" answer.

In a code like the previous one, if I know for sure that n + x is positive, can I assume that the sum of unsigned int n and int x gives the expected value?

geza

In a code like the previous one, if I know for sure that n + x is positive, can I assume that the sum of unsigned int n and int x gives the expected value?

Yes.

First, the signed value converted to unsigned, using modulo arithmetic:

If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type).

Then two unsigned values will be added using modulo arithmetic:

Unsigned integers shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.

This means that you'll get the expected answer.

Even, if the result would be negative in the mathematical sense, the result in C++ would be a number which is modulo-equal to the negative number.

Note that I've supposed here that you add two same-sized integers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ Implicit Conversion (Signed + Unsigned)

From Dev

Inconsistent behaviour of implicit conversion between unsigned and bigger signed types

From Dev

Signed to unsigned conversion in C++

From Dev

Conversion between signed integer and unsigned integer

From Dev

Type conversion: signed int to unsigned long in C

From Dev

C++: <= conflict between signed and unsigned

From Java

C arithmetic conversion multiplying unsigned with signed and result in float

From Dev

C++ signed integer conversion to unsigned with more bits

From Dev

C# signed and unsigned integers are Narrowing conversion both ways?

From Dev

Forbidding implicit `unsigned` to `double` conversion

From Dev

Qualification of code - comparisons between signed and unsigned values in libc's qsort

From Dev

Preventing code duplication between signed and unsigned integers logic

From Dev

how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?

From Dev

how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?

From Dev

On narrowing casts in C# between signed and unsigned integral types

From Dev

c++: Comparison between signed and unsigned integer expressions

From Dev

what is the difference between warnings C4018 ('expression' : signed/unsigned mismatch) and C4389 ('operator' : signed/unsigned mismatch)

From Java

Type conversion - unsigned to signed int/char

From Dev

What is happening with signed/unsigned int conversion?

From Dev

Signed Integer to Unsigned Integer & DWORD Conversion

From Dev

conversion of unsigned integer variable to signed variable

From Dev

Signed Integer to Unsigned Integer & DWORD Conversion

From Dev

conversion of unsigned integer variable to signed variable

From Dev

implicit conversion of 'unsigned long 'UIUserNotificationSettings *' is disallowed with arc

From Dev

implicit conversion changes signedness 'int" to 'unsigned int"

From Dev

implicit conversion of 'unsigned long 'UIUserNotificationSettings *' is disallowed with arc

From Dev

C++ cast unsigned to signed

From Dev

signed or unsigned int in c++

From Dev

signed or unsigned int in c++

Related Related

  1. 1

    C++ Implicit Conversion (Signed + Unsigned)

  2. 2

    Inconsistent behaviour of implicit conversion between unsigned and bigger signed types

  3. 3

    Signed to unsigned conversion in C++

  4. 4

    Conversion between signed integer and unsigned integer

  5. 5

    Type conversion: signed int to unsigned long in C

  6. 6

    C++: <= conflict between signed and unsigned

  7. 7

    C arithmetic conversion multiplying unsigned with signed and result in float

  8. 8

    C++ signed integer conversion to unsigned with more bits

  9. 9

    C# signed and unsigned integers are Narrowing conversion both ways?

  10. 10

    Forbidding implicit `unsigned` to `double` conversion

  11. 11

    Qualification of code - comparisons between signed and unsigned values in libc's qsort

  12. 12

    Preventing code duplication between signed and unsigned integers logic

  13. 13

    how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?

  14. 14

    how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?

  15. 15

    On narrowing casts in C# between signed and unsigned integral types

  16. 16

    c++: Comparison between signed and unsigned integer expressions

  17. 17

    what is the difference between warnings C4018 ('expression' : signed/unsigned mismatch) and C4389 ('operator' : signed/unsigned mismatch)

  18. 18

    Type conversion - unsigned to signed int/char

  19. 19

    What is happening with signed/unsigned int conversion?

  20. 20

    Signed Integer to Unsigned Integer & DWORD Conversion

  21. 21

    conversion of unsigned integer variable to signed variable

  22. 22

    Signed Integer to Unsigned Integer & DWORD Conversion

  23. 23

    conversion of unsigned integer variable to signed variable

  24. 24

    implicit conversion of 'unsigned long 'UIUserNotificationSettings *' is disallowed with arc

  25. 25

    implicit conversion changes signedness 'int" to 'unsigned int"

  26. 26

    implicit conversion of 'unsigned long 'UIUserNotificationSettings *' is disallowed with arc

  27. 27

    C++ cast unsigned to signed

  28. 28

    signed or unsigned int in c++

  29. 29

    signed or unsigned int in c++

HotTag

Archive