473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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.
Nov 18 '08 #1
4 4047
pa********@gmail.com wrote:
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?
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
Nov 18 '08 #2
On Nov 18, 9:22*pm, pasamdi...@gmail.com wrote:
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;
That may or may work (then memcpy'ing the results); check your
compilers documentation.
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.
That's the usual solution.
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:ja*********@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
Nov 18 '08 #3
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
<pa********@gmail.comwrote in message
news:89**********************************@s9g2000p rg.googlegroups.com...
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.

Nov 19 '08 #4
On Nov 19, 9:24 am, "Bill Davy" <B...@XchelSys.co.ukwrote:
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
Maybe. Trying to access it is undefined behavior, so there's no
way to tell.

--
James Kanze (GABI Software) email:ja*********@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
Nov 19 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
9
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
20
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include...
5
by: Mitchell S. Honnert | last post by:
I'm thinking about adding unsigned types, like UShort and UInt, to a VB.NET library that I published which edits ID3 tag information. It would make the interface much more clean to have some...
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
4
by: techie | last post by:
I have defined a number of unsigned integer types as follows: typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedfe long long uint64; Is it...
10
by: krunalb | last post by:
Hi, I am trying to shift unsigned long long value by 64 bits and this is what i get #include <stdio.h> int main() { unsigned short shiftby= 64;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.