Like Tree1Likes
  • 1 Post By Salem

two uint16 types to uint32

This is a discussion on two uint16 types to uint32 within the C Programming forums, part of the General Programming Boards category; Hi, Got a problem to solve. How to do it? Must use most significant and least significant. Lets say uint16 ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    7

    two uint16 types to uint32

    Hi,

    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
    Posts
    1,342
    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
    Location
    The edge of the known universe
    Posts
    32,855
    Something like
    uint32 r = ((uint32)u16X << 16) | u16Y;
    vart likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,676
    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, Sunshine, 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, 09: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, 04:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21