473,382 Members | 1,313 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,382 software developers and data experts.

Bit Field Question

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!
Jul 22 '05 #1
5 4642

Marc Ueberall wrote:
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-

Jul 22 '05 #2
shez wrote:
Marc Ueberall wrote:
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-


Thanks a lot! That works great! :D
Jul 22 '05 #3
shez wrote:
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
Jul 22 '05 #4

Patrick Leslie Polzer wrote:
shez wrote:
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.


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

Jul 22 '05 #5
Marc Ueberall wrote:
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

Jul 22 '05 #6

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

Similar topics

10
by: Andres Eduardo Hernando | last post by:
Hi, I'm not entirely sure this is the right group to ask this question, but I saw a similar one above, and the group's charter is not clear enough about it, so, here I go: ;) What is the...
25
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the...
1
by: Mal | last post by:
Hello. I have inherited a badly designed database and am trying to improve it. It is basically a flat file for survey results. one table holds, Name, Age etc as well as a field for each...
3
by: DaveC | last post by:
Hi.. I have a C exam in a few days.. Looking back at old exams I find a few things we have not been taught / cant remember being taught. Below is a question from last year. My question is how do...
4
by: Woody Splawn | last post by:
I have a grid with, lets say, 3 fields in it, Debit, Credit and Comments, in that order. When the user moves to the debit field, if he enters data in it, I would like for the cursor to skip past...
1
by: Wei Wang | last post by:
Hi, Sorry for asking the dynamic command question without thoroughly reading the documentation first. Thanks a lot for your reply, Richard. ;-) Now this may be a naive SQL question: Is there...
3
by: Microsoft_Public | last post by:
All I'm getting is <null>...... I have a legacy input form that I must maintain for a few more months until the balance of the site can be converted to .Net. I need the one database field to...
1
by: LurfysMa | last post by:
I am working on an electronic flashcard program. Most of the subjects are simple lists of questions and answers. Those seem to be working. Some of the "subjects" have "categories" of questions. ...
9
by: Brett_A | last post by:
I have a form where the first field is a dynamic drop-down that pulls from a db (Access). The fields associated with the query are task_id, task_name and task_rate. The field has the value of...
12
by: jayjayplane | last post by:
Thanks everybody here answered my last data field validation question about the alert of future date. Here I get another question, sorry, I am really an Access newbie... The question is : I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.