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

Samurai Jack

As per the MSDN article: https://msdn.microsoft.com/en-us/library/8s682k58%28v=vs.80%29.aspx

Explicit conversion is required by some compilers to support narrowing conversions..

Based on the statements made in the above msdn link, is it safe to say that

Converting int to uint is narrowing conversion

Also,

Converting uint to int is narrowing conversion

?

Matthew Watson

Converting an integral or floating point type from signed to unsigned and vice-versa is neither a narrowing nor a widening conversion, since the number of bits used to store it remains unchanged.

Instead, it is a change of representation and can utterly change the number (e.g. signed -1 is converted to unsigned 0xffffffff).

In fact, if you use unchecked arithmetic:

unchecked
{
    int  x = -1;
    uint y = (uint) x;
    int  z = (int) y;

    Debug.Assert(x == z); // Will succeed for all x.

    uint a = 0xffffffff;
    int  b = (int) a;
    uint c = (uint) b;

    Debug.Assert(a == c); // Will succeed for all a.
}

So a round trip works in both directions, which proves that narrowing does not occur in either direction.

Narrowing and widening only applies to integral and floating point types where a different number of bits are used to store one or more parts of the number.

However, because the number's value can be changed, you must cast to make such a conversion, to ensure that you don't do it accidentally.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

bitwise NOT of signed and unsigned integers in C

From Dev

Signed to unsigned conversion in C++

From Dev

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

From Dev

Casting both bitwidth and signed/unsigned, which conversion is executed first?

From Dev

Type conversion: signed int to unsigned long in C

From Dev

C++ Implicit Conversion (Signed + Unsigned)

From Dev

Signed and Unsigned Integers

From Dev

Narrowing conversion in C++

From Dev

How does C store negative numbers in signed vs unsigned integers?

From Dev

Signed result of subtracted unsigned integers?

From Dev

Adding to unsigned and signed binary integers

From Dev

Narrowing conversion from 'long' to signed type 'char' is implementation-defined (strtol function in C)

From Dev

narrowing conversion from int to long unsigned int {} is ill-formed in C++11

From Java

c++ safeness of code with implicit conversion 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

Why is "unsigned int ui = {-1};" a narrowing conversion error?

From Dev

"comparison between signed and unsigned integer expressions" with only unsigned integers

From Dev

"comparison between signed and unsigned integer expressions" with only unsigned integers

From Java

Type conversion - unsigned to signed int/char

From Dev

What is happening with signed/unsigned int conversion?

From Dev

Conversion between signed integer and unsigned integer

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

C++11: "narrowing conversion inside { }" with modulus

From Dev

warning: narrowing conversion C++11

From Dev

C++ cast unsigned to signed

Related Related

  1. 1

    bitwise NOT of signed and unsigned integers in C

  2. 2

    Signed to unsigned conversion in C++

  3. 3

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

  4. 4

    Casting both bitwidth and signed/unsigned, which conversion is executed first?

  5. 5

    Type conversion: signed int to unsigned long in C

  6. 6

    C++ Implicit Conversion (Signed + Unsigned)

  7. 7

    Signed and Unsigned Integers

  8. 8

    Narrowing conversion in C++

  9. 9

    How does C store negative numbers in signed vs unsigned integers?

  10. 10

    Signed result of subtracted unsigned integers?

  11. 11

    Adding to unsigned and signed binary integers

  12. 12

    Narrowing conversion from 'long' to signed type 'char' is implementation-defined (strtol function in C)

  13. 13

    narrowing conversion from int to long unsigned int {} is ill-formed in C++11

  14. 14

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

  15. 15

    C arithmetic conversion multiplying unsigned with signed and result in float

  16. 16

    C++ signed integer conversion to unsigned with more bits

  17. 17

    Why is "unsigned int ui = {-1};" a narrowing conversion error?

  18. 18

    "comparison between signed and unsigned integer expressions" with only unsigned integers

  19. 19

    "comparison between signed and unsigned integer expressions" with only unsigned integers

  20. 20

    Type conversion - unsigned to signed int/char

  21. 21

    What is happening with signed/unsigned int conversion?

  22. 22

    Conversion between signed integer and unsigned integer

  23. 23

    Signed Integer to Unsigned Integer & DWORD Conversion

  24. 24

    conversion of unsigned integer variable to signed variable

  25. 25

    Signed Integer to Unsigned Integer & DWORD Conversion

  26. 26

    conversion of unsigned integer variable to signed variable

  27. 27

    C++11: "narrowing conversion inside { }" with modulus

  28. 28

    warning: narrowing conversion C++11

  29. 29

    C++ cast unsigned to signed

HotTag

Archive