Typecasting a negative number to positive in C

cpd1

I wouldn't expect the value that gets printed to be the initial negative value. Is there something I'm missing for type casting?

#include<stdint.h>


int main() {

    int32_t color = -2451337;
    uint32_t color2 = (uint32_t)color;

    printf("%d", (uint32_t)color2);
    return 0;
}
Keith Thompson
int32_t color = -2451337;
uint32_t color2 = (uint32_t)color;

The cast is unnecessary; if you omit it, exactly the same conversion will be done implicitly.

For any conversion between two numeric types, if the value is representable in both types, the conversion preserves the value. But since color is negative, that's not the case here.

For conversion from a signed integer type to an unsigned integer type, the result is implementation-defined (or it can raise an implementation-defined signal, but I don't know of any compiler that does that).

Under most compilers, conversions between integer types of the same size just copies or reinterprets the bits making up the representation. The standard requires int32_t to use two's-complement representation, so if the conversion just copies the bits, then the result will be 4292515959.

(Other results are permitted by the C standard, but not likely to be implemented by real-world compilers. The standard permits one's-complement and sign-and magnitude representations for signed integer types, but specifically requires int32_t to use two's-complement; a C compiler for a one's complement CPU probably just would't define int32_t.)

printf("%d", (uint32_t)color2);

Again, the cast is unnecessary, since color2 is already of type uint32_t. But the "%d" format requires an argument of type int, which is a signed type (that may be as narrow as 16 bits). In this case, the uint32_t value isn't converted to int. Most likely the representation of color2 will be treated as if it were an int object, but the behavior is undefined, so as far as the C standard is concerned quite literally anything could happen.

To print a uint32_t value, you can use the PRId32 macro defined in <inttypes.h>:

printf("%" PRId32, color32);

Or, perhaps more simply, you can convert it to the widest unsigned integer type and use "%ju":

printf("%ju", (uintmax_t)color32);

This will print the implementation-defined value (probably 4292515959) of color32.

And you should add a newline \n to the format string.

More quibbles:

You're missing #include <stdio.h>, which is required if you call printf.

int main() is ok, but int main(void) is preferred.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Typecasting a negative number to positive in C

From Dev

Rounding a Positive or Negative Number

From Dev

How to Convert Positive or Negative Scientific Notation to Number in C#?

From Dev

Array Of Random Alternate Positive And Negative Number in C#

From Dev

Modulus between positive and negative number

From Dev

AngularJs show negative or positive number

From Dev

Bitwise operation positive number to negative

From Dev

Detect if number change from negative to positive or positive to negative

From Dev

Detect if number change from negative to positive or positive to negative

From Dev

Convert negative number to positive number in django template?

From Dev

Convert positive number to a negative number - iOS

From Dev

Convert positive number to a negative number - iOS

From Dev

python string format with negative sign for negative number, but space for positive number

From Dev

find matches for positive and negative number in arithmetic expression

From Dev

how to sum positive (+) and negative (-) number in excel

From Dev

PROLOG-number of positive and negative numbers

From Dev

how to find negative and positive number from a string?

From Dev

If positive number say yes if negative say no

From Dev

Mathematically checking if a number is positive, negative or 0

From Dev

Format to show positive or negative sign of a number in java

From Dev

Number Of Rows Since Positive/Negative in Pandas

From Dev

String slicing from a negative to a positive number

From Dev

Positive integers in array sum to a negative number

From Dev

How to convert a positive number to negative in batch?

From Dev

Normalize a negative or positive number between -1 and 1?

From Dev

how to group data by positive or negative number in pandas

From Dev

How to make my number turn positive/negative based of D/C column?

From Dev

I want program to close if number is negative but it closes even if the number is positive

From Dev

AS3 convert a positive number to 1 and a negative number to -1

Related Related

  1. 1

    Typecasting a negative number to positive in C

  2. 2

    Rounding a Positive or Negative Number

  3. 3

    How to Convert Positive or Negative Scientific Notation to Number in C#?

  4. 4

    Array Of Random Alternate Positive And Negative Number in C#

  5. 5

    Modulus between positive and negative number

  6. 6

    AngularJs show negative or positive number

  7. 7

    Bitwise operation positive number to negative

  8. 8

    Detect if number change from negative to positive or positive to negative

  9. 9

    Detect if number change from negative to positive or positive to negative

  10. 10

    Convert negative number to positive number in django template?

  11. 11

    Convert positive number to a negative number - iOS

  12. 12

    Convert positive number to a negative number - iOS

  13. 13

    python string format with negative sign for negative number, but space for positive number

  14. 14

    find matches for positive and negative number in arithmetic expression

  15. 15

    how to sum positive (+) and negative (-) number in excel

  16. 16

    PROLOG-number of positive and negative numbers

  17. 17

    how to find negative and positive number from a string?

  18. 18

    If positive number say yes if negative say no

  19. 19

    Mathematically checking if a number is positive, negative or 0

  20. 20

    Format to show positive or negative sign of a number in java

  21. 21

    Number Of Rows Since Positive/Negative in Pandas

  22. 22

    String slicing from a negative to a positive number

  23. 23

    Positive integers in array sum to a negative number

  24. 24

    How to convert a positive number to negative in batch?

  25. 25

    Normalize a negative or positive number between -1 and 1?

  26. 26

    how to group data by positive or negative number in pandas

  27. 27

    How to make my number turn positive/negative based of D/C column?

  28. 28

    I want program to close if number is negative but it closes even if the number is positive

  29. 29

    AS3 convert a positive number to 1 and a negative number to -1

HotTag

Archive