Connecting Tech Pros Worldwide Help | Site Map

how to place two unsigned 16 bit values into a 32 bit unsigned value

pasamdivya@gmail.com
Guest
 
Posts: n/a
#1: Nov 18 '08
Hi everyone,

I need to store an unsigned 16 bit number in the upper (MSB) and
another unsigned 16 bit number in the 16 LSB. Basically, I am trying
to store two unsigned 16 bit numbers into one unsigned 32 bit number.

So far, I have the following:

struct
{
unsigned int num1:16;
unsigned in num2:16;
} twoUnsigned16BitNums;

unsigned int num;

What I am having trouble with is to store the two 16 bit numbers into
the 32 bit number. I know one option is to use bit wise ANDs and ORs.
Any help on your part is greatly appreciated. Best Regards.
Victor Bazarov
Guest
 
Posts: n/a
#2: Nov 18 '08

re: how to place two unsigned 16 bit values into a 32 bit unsigned value


pasamdivya@gmail.com wrote:
Quote:
I need to store an unsigned 16 bit number in the upper (MSB) and
another unsigned 16 bit number in the 16 LSB. Basically, I am trying
to store two unsigned 16 bit numbers into one unsigned 32 bit number.
>
So far, I have the following:
>
struct
{
unsigned int num1:16;
unsigned in num2:16;
} twoUnsigned16BitNums;
>
unsigned int num;
>
What I am having trouble with is to store the two 16 bit numbers into
the 32 bit number. I know one option is to use bit wise ANDs and ORs.
It would seem that you're hesitant to use that option. May I ask, why?
Quote:
Any help on your part is greatly appreciated. Best Regards.
Use a plus and a multiply.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
James Kanze
Guest
 
Posts: n/a
#3: Nov 18 '08

re: how to place two unsigned 16 bit values into a 32 bit unsigned value


On Nov 18, 9:22*pm, pasamdi...@gmail.com wrote:
Quote:
I need to store an unsigned 16 bit number in the upper (MSB)
and another unsigned 16 bit number in the 16 LSB. Basically, I
am trying to store two unsigned 16 bit numbers into one
unsigned 32 bit number.
Quote:
So far, I have the following:
Quote:
struct
{
* *unsigned int num1:16;
* *unsigned in num2:16;
} twoUnsigned16BitNums;
That may or may work (then memcpy'ing the results); check your
compilers documentation.
Quote:
unsigned int num;
Quote:
What I am having trouble with is to store the two 16 bit
numbers into the 32 bit number. I know one option is to use
bit wise ANDs and ORs.
That's the usual solution.
Quote:
Any help on your part is greatly appreciated.
Is "number1 << 16 | number2" what you're looking for? (Just
make sure that 32 bit arithmetic is used here. If number1 is an
unsigned int, and int's are only 16 bits, you'll have to cast it
to unsigned long first.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Bill Davy
Guest
 
Posts: n/a
#4: Nov 19 '08

re: how to place two unsigned 16 bit values into a 32 bit unsigned value


Depending on portability (compiler, processor, packing, etc) you may be able
to use a union. For VS on Intel

union

{

struct

{

unsigned a:16;

unsigned b:16;

} Half;

struct

{

unsigned ab:32;

} Whole;

} t;

t.Half.a = 0x1234;

t.Half.b = 0x5678;

t.Whole.ab is now 0x56781234


<pasamdivya@gmail.comwrote in message
news:89f27f84-c5aa-4af8-a4f8-89f2984432bd@s9g2000prg.googlegroups.com...
Quote:
Hi everyone,
>
I need to store an unsigned 16 bit number in the upper (MSB) and
another unsigned 16 bit number in the 16 LSB. Basically, I am trying
to store two unsigned 16 bit numbers into one unsigned 32 bit number.
>
So far, I have the following:
>
struct
{
unsigned int num1:16;
unsigned in num2:16;
} twoUnsigned16BitNums;
>
unsigned int num;
>
What I am having trouble with is to store the two 16 bit numbers into
the 32 bit number. I know one option is to use bit wise ANDs and ORs.
Any help on your part is greatly appreciated. Best Regards.

James Kanze
Guest
 
Posts: n/a
#5: Nov 19 '08

re: how to place two unsigned 16 bit values into a 32 bit unsigned value


On Nov 19, 9:24 am, "Bill Davy" <B...@XchelSys.co.ukwrote:
Quote:
Depending on portability (compiler, processor, packing, etc)
you may be able to use a union. For VS on Intel
Quote:
union
{
struct
{
unsigned a:16;
unsigned b:16;
} Half;
Quote:
struct
{
unsigned ab:32;
} Whole;
} t;
Quote:
t.Half.a = 0x1234;
t.Half.b = 0x5678;
Quote:
t.Whole.ab is now 0x56781234
Maybe. Trying to access it is undefined behavior, so there's no
way to tell.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Closed Thread