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
and the answer is 165.
Thanks!!!!!
Printable View
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
and the answer is 165.
Thanks!!!!!
Generally I use "=" for that.
-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.
u32? How do you have a negative unsigned integer?
The conversion from signed integer to unsigned integer results in a change of value, not (necessarily?) a change in the underlying bits.Quote:
Originally Posted by itCbitC
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.Quote:
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. 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".
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.Quote:
Originally Posted by itCbitC
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.Quote:
Originally Posted by itCbitC
Your point of information in no way contradicts bithub's explanation that:
If anything, your point of information confirms bithub's assertion.Quote:
Originally Posted by bithub
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.
The classic example would be "10000000" as noted before which can be signed or unsigned depending on the compiler's interpretation.
I'm not too sure about that.
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.
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.