Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading bits from stream. 1

Status
Not open for further replies.

countdrak

Programmer
Jun 20, 2003
358
0
0
US
The file format is such that in every 32 bits 1st 2 bits are control and next 10 are signal3, the next 10 signal2, last 10 is signal1.

How do I read in the 32 bits? Have no idea, so any idea would be appreciated.
 
There are many ways to do this but the most important thing will be how you process the data.
To do this you will need a bitfield structure (or, if you are feeling brave, you could manually mask out the bits for each field individually).
So in your case you might like a structure like this:
Code:
typedef struct {
        unsigned    control : 2,
                    signal3 : 10,
                    signal2 : 10,
                    signal1 : 10   } bits;
Then you can use a "bits" pointer and refer to the the relevant 32 bit word and register in it.


Trojan.
 
Note: You might have to reverse the order of the fields in the structure depending upon whether your processor is big-endian or little-endian.


Trojan.
 
Thanks. That is exactly how I started off, but got lost in the middle. I should scratch it and start again.
 
Strictly speaking, C does not define bit field order at all. So it seems structure bit field is (almost;) useless concept. Better use shift and mask (bitwise ops) approach: it's more safe method for the platform-dependent operations...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top