# u32 to u8

This is a discussion on u32 to u8 within the C Programming forums, part of the General Programming Boards category; HI, Just wondering if anyone knows what is the operations for converting a u32 number to u8, for example, convert ...

1. ## u32 to u8

HI,
Just wondering if anyone knows what is the operations for converting a u32 number to u8,
for example, convert (u32) a = -1515870811 to (u8) a

Thanks!!!!!

2. Generally I use "=" for that.

3. -1515870811 in hex is -0x5A5A5A5B. In memory, this is represented as the two's compliment of 0xA5A5A5A5. Since you are placing this in a 8 bit value, only the least significant byte is saved. This is 0xA5 which is 165 decimal.

4. u32? How do you have a negative unsigned integer?

5. Originally Posted by Epy
u32? How do you have a negative unsigned integer?
It doesn't matter (in regards to this question). In memory, it is represented the same regardless of the type.

6. Originally Posted by bithub
It doesn't matter (in regards to this question). In memory, it is represented the same regardless of the type.
IBTD since the binary representations of signed and unsigned numbers would be poles apart.

7. Originally Posted by itCbitC
IBTD since the binary representations of signed and unsigned numbers would be poles apart.
The conversion from signed integer to unsigned integer results in a change of value, not (necessarily?) a change in the underlying bits.

8. Originally Posted by laserlight
The conversion from signed integer to unsigned integer results in a change of value, not (necessarily?) a change in the underlying bits.
IBTD since at the vey least the sign bit is flipped, and to get the 2's complement all bits are flipped and one added to the result.

9. Originally Posted by itCbitC
IBTD since at the vey least the sign bit is flipped, and to get the 2's complement all bits are flipped and one added to the result.
You are thinking of how to obtain the 2's complement representation of a negative number by starting from its magnitude, e.g., for a 4 bit integer, the representation for -2 can be obtained from the representation of 2 via 0010 -> 1101 -> 1101 + 1 -> 1110. However, -2 when converted to a 4 bit unsigned integer according to the rules of C would be 14, not 2. The representation of 14 in a 4 bit unsigned integer is 1110.

10. Originally Posted by itCbitC
IBTD since at the vey least the sign bit is flipped, and to get the 2's complement all bits are flipped and one added to the result.
Classic example:
Negate -128 stored in a byte.

11. Originally Posted by iMalc
Classic example:
Negate -128 stored in a byte.
An arbitrary bit pattern stored in a location can be construed as signed or unsigned depending on the compiler unless the program context explicitly states signed or unsigned.

12. Originally Posted by laserlight
You are thinking of how to obtain the 2's complement representation of a negative number by starting from its magnitude, e.g., for a 4 bit integer, the representation for -2 can be obtained from the representation of 2 via 0010 -> 1101 -> 1101 + 1 -> 1110. However, -2 when converted to a 4 bit unsigned integer according to the rules of C would be 14, not 2. The representation of 14 in a 4 bit unsigned integer is 1110.
All I'm trying to say is that the compiler is free to interpret it depending on its default rules unless the program context explicitly clarifies its usage ie signed or unsigned. Given the above example, when the compiler sees "1110" it's free to choose whether this bit pattern is signed or unsigned. Some may interpret it as "-2" others may interpret it as "14".

13. Originally Posted by itCbitC
All I'm trying to say is that the compiler is free to interpret it depending on its default rules unless the program context explicitly clarifies its usage ie signed or unsigned.
In the context that bithub was referring to, "the program context explicitly clarifies its usage", i.e., the integer resulting from the expression -1515870811 is signed, but the variable a that the expression is assigned to is unsigned. The conversion from signed integer to unsigned integer is well defined.

Originally Posted by itCbitC
Given the above example, when the compiler sees "1110" it's free to choose whether this bit pattern is signed or unsigned. Some may interpret it as "-2" others may interpret it as "14".
Given just a bit pattern, I think that the compiler is not free to choose anything. It is just a bit pattern. Type information must be present.

Originally Posted by bithub
It doesn't matter (in regards to this question). In memory, it is represented the same regardless of the type.
If anything, your point of information confirms bithub's assertion.

14. Originally Posted by laserlight
In the context that bithub was referring to, "the program context explicitly clarifies its usage", i.e., the integer resulting from the expression -1515870811 is signed, but the variable a that the expression is assigned to is unsigned. The conversion from signed integer to unsigned integer is well defined.
Ultimately it's all smoke 'n mirrors. The point I'm trying to make is why assign -1515870811 to an unsigned int which would be interpreted as 2779096485, whose bit pattern has nothing in common with 1515870811 unless the machine uses sign-magnitude notation internally to represent negative numbers instead of 2's complement notation but even then the MSB would be flipped.
Originally Posted by laserlight
Given just a bit pattern, I think that the compiler is not free to choose anything. It is just a bit pattern. Type information must be present.
The classic example would be "10000000" as noted before which can be signed or unsigned depending on the compiler's interpretation.
Originally Posted by laserlight

If anything, your point of information confirms bithub's assertion.
I'm not too sure about that.

15. Originally Posted by itCbitC
Ultimately it's all smoke 'n mirrors. The point I'm trying to make is why assign -1515870811 to an unsigned int which would be interpreted as 2779096485, whose bit pattern has nothing in common with 1515870811 unless the machine uses sign-magnitude notation internally to represent negative numbers instead of 2's complement notation but even then the MSB would be flipped.
I think you're the only one who is trying to relate -1515870811 to +1515870811. It's not unheard of to do something like "unsigned_variable = -1" to get all-bits-on, for example.

Originally Posted by itCbitC
The classic example would be "10000000" as noted before which can be signed or unsigned depending on the compiler's interpretation.
And? The same thing is true about "10101010", and "11001100", and every other bit pattern that starts with 1. The bit pattern is still just a bit pattern; only when it is used (as either an unsigned value or a signed value) do we have to become paranoid about which it is.

Page 1 of 2 12 Last