Bitfields

KansaiRobot

Junior Member
May 1, 2012
21
0
61
Hello.
I am currently updating a project that uses bitfields heavily. To do this I am planning to use these as well. My question is if the following is correct, appropriate or a good idea:


first a field in the bitfield:


Code:
struct {
WORD somefield:1;
WORD otherfield :1;


WORD MYFIELD:2; // The 2-bit field I am going to use


} somebitfield;

Yeah, I know the standard recommends only use unsigned ints for that, but for some reason the project uses a user defined WORD.


Ok, so I have a 2-bit field so I guess it means this can assume 4 values right?


So I define something like


Code:
 enum { NOTHING=0, STATEONE, STATETWO,STATETHREE} myenum;


So, my question is , can I safely do something like


Code:
if(somebitfield.MYFIELD==NOTHING) {


}
elseif (somebitfield.MYFIELD==STATETWO){


}


or even


Code:
switch(somebitfield.MYFIELD)
  {
   case NOTHING:   


  case STATEONE:


}


any advice will be greatly appreciated


Thanks


Kansai
 

The J

Senior member
Aug 30, 2004
755
0
76
Yup, that should work as the compiler should set up the necessary masks and shifts for you. At least, that's been my experience with the compiler I work with (Microchip C30 and C32, which are based on GCC).
 

KansaiRobot

Junior Member
May 1, 2012
21
0
61
thanks for the replies.
I already implemented the solution and it works great. I was just a little aprehensive about bitfields and enums since I seldom use them.

(We are taking care of concurrency by deactivating interrupts in atomic operations and activating them again later.. at least that is what the code does)


BY THE WAY, talking about bit fields. The standard recommends to use only unsigned ints. However my legacy code uses WORD and sometimes BYTE (another user defined type)

Since bit fields are just that: bits, I always wonder what for are these types? I mean for example:


Code:
struct{
        unsigned int field1:1;
        unsigned int field2:2;
       ...

field1 is just one bit, and field2 just two bits. Then why put the "int" there???

Any idea?
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
The underlying machine is incapable of single-bit operations. The compiler emulates them by performing full-uint loads, shifts, masks, and then full-uint stores. 'unsigned int' is the underlying access size.

Edit: This means the entire unsigned int is actually there, but only those two bits in the uint ever appear to change.
 
Last edited:

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
The underlying machine is incapable of single-bit operations. The compiler emulates them by performing full-uint loads, shifts, masks, and then full-uint stores. 'unsigned int' is the underlying access size.

Edit: This means the entire unsigned int is actually there, but only those two bits in the uint ever appear to change.

Interesting. I always thought it packed the bits into a single scalar value.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |