Thread: two uint16 types to uint32

  1. #1
    Registered User
    Join Date
    Jul 2013

    two uint16 types to uint32


    Got a problem to solve. How to do it? Must use most significant and least significant. Lets say uint16 u16X = 10; and uint16 u16Y = 8; Now I must convert it into a uint32, which holds both of these values. Bit logic ... I have to put one number ahead of other. Can't figure it out. I hope you guys understand.

  2. #2
    Registered User
    Join Date
    Apr 2013
    You could use a union to do this and let the compiler figure it out, or you could manually write the code to put the two uint16's into a uint32 (one in the upper 16 bits, the other in the lower 16 bits).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Something like
    uint32 r = ((uint32)u16X << 16) | u16Y;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Or, you could do it with numeric operations rather than by bit fiddling. Just make use of the fact that numeric operations on unsigned types work with modulo arithmetic.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read uint8, uint16 and uint32 from a buffer
    By evariste in forum C Programming
    Replies: 6
    Last Post: 11-26-2010, 12:32 PM
  2. ‘UInt16’ does not name a type
    By Jelte in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2010, 08:59 AM
  3. 2 uint8_t to a uint16?
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 02-26-2009, 06:40 AM
  4. concatenating two bytes into an uint16
    By davo666 in forum C Programming
    Replies: 11
    Last Post: 01-07-2009, 11:43 PM
  5. UINT32 x : 1 ?
    By mottoshiritai in forum C Programming
    Replies: 3
    Last Post: 06-25-2008, 03:53 AM