Bit Field Question

M

Marc Ueberall

Hi there!

I've got the following problem. I have to read an integer value out of a
2 byte data block. The structure is like following:

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
x y |---- numeric data ----|

Bit 15 and 14 are just some flags. I've got to get the numeric data
encoded in the bits 13-0.

I tried this:

struct BitField
{
unsigned short x: 1;
unsigned short y: 1;
unsigned short value: 14;
};

struct BitField *testField= (struct BitField*)aValue;

aValue->value...

But this didn't work.

Does someone knows how to get the numeric data???

Thanks a lot for your help!
 
S

shez

Marc said:
Hi there!

I've got the following problem. I have to read an integer value out of a
2 byte data block. The structure is like following:

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
x y |---- numeric data ----|

Bit 15 and 14 are just some flags. I've got to get the numeric data
encoded in the bits 13-0.

I tried this:

struct BitField
{
unsigned short x: 1;
unsigned short y: 1;
unsigned short value: 14;
};

struct BitField *testField= (struct BitField*)aValue;

aValue->value...

But this didn't work.

Does someone knows how to get the numeric data???

Thanks a lot for your help!

What error are you getting? It might be an alignment problem (i
think). Instead of:

struct BitField *testField= (struct BitField*)aValue;

Try:

BitField testField;
std::memcpy(&testField, aValue, sizeof(testField));

I'm using C++ syntax here instead of C syntax (this is, after all, a
C++ group).

-shez-
 
M

Marc Ueberall / DuskEngine

shez said:
of a



What error are you getting? It might be an alignment problem (i
think). Instead of:

struct BitField *testField= (struct BitField*)aValue;

Try:

BitField testField;
std::memcpy(&testField, aValue, sizeof(testField));

I'm using C++ syntax here instead of C syntax (this is, after all, a
C++ group).

-shez-

Thanks a lot! That works great! :D
 
P

Patrick Leslie Polzer

shez said:
Try:

BitField testField;
std::memcpy(&testField, aValue, sizeof(testField));
Umm... is that portable? I'd say it would be a cleaner way to mask out
the unwanted bits.

Leslie
 
M

msalters

Patrick said:
Umm... is that portable? I'd say it would be a cleaner way to mask out
the unwanted bits.

50/50 in the real world; it won't work on the compilers which
allocate bitfield from the other end i.e. end up with 14:1:1

Bitmasking of course is possible and in this case a lot easier.
We really need something like std::bits<from,to>(int&) so we
can say
int x=1;
bits<0,13>(x)=0;
bits<14,14>(x) = 3; // sets precisely one bit
int a = bits(x); // a == 1<<14

It's simple and yet will encourage proper programming.
Regards,
Michiel Salters
 
T

Thomas Matthews

Marc said:
Hi there!

I've got the following problem. I have to read an integer value out of a
2 byte data block. The structure is like following:

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
x y |---- numeric data ----|

Bit 15 and 14 are just some flags. I've got to get the numeric data
encoded in the bits 13-0.

I tried this:

struct BitField
{
unsigned short x: 1;
unsigned short y: 1;
unsigned short value: 14;
};

struct BitField *testField= (struct BitField*)aValue;

aValue->value...

But this didn't work.

Does someone knows how to get the numeric data???

Thanks a lot for your help!

What about this:
value = high_byte & 0x3F + low_byte;
or
value = two_byte_value & 0x3FFF;


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top