Thread: u32 to u8

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    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
    and the answer is 165.


    Thanks!!!!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Generally I use "=" for that.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    -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.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    u32? How do you have a negative unsigned integer?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Epy View Post
    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.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by bithub View Post
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    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. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Last edited by laserlight; 10-22-2009 at 05:30 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by itCbitC View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by iMalc View Post
    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. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    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. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    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 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.

    Your point of information in no way contradicts bithub's explanation that:
    Quote 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.
    Last edited by laserlight; 10-22-2009 at 01:13 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    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.
    Quote Originally Posted by laserlight View Post
    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.
    Quote Originally Posted by laserlight View Post
    Your point of information in no way contradicts bithub's explanation that:

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

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by itCbitC View Post
    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.

    Quote Originally Posted by itCbitC View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer + struct w/arrays confusion
    By Viper187 in forum C Programming
    Replies: 1
    Last Post: 07-23-2009, 09:37 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Swapping endiannes
    By Rennor in forum C Programming
    Replies: 3
    Last Post: 09-05-2006, 03:13 AM
  5. struct constructor woes
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2004, 03:36 PM