View previous topic :: View next topic |
Author |
Message |
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
how to test for bits in a structure |
Posted: Thu Sep 06, 2007 2:23 am |
|
|
I am trying to test bits in a structure, can anyone help ?
this fails with " element is not a member".
how do I do a bit test to get a true/false ?
code --------------------
if (!current_data.Sequential_Shutter_Active_flag) // not active
{
// do something 'cos the bit is 0 not 1
}
structure definition ------------
struct data
{
long magicnumber;
signed level;
signed gain;
signed long ref_os;
int ext_ref_freq;
int address;
signed ref_state;
short NTSC_60Hz_flag;
short Sequential_Shutter_Active_flag;
short rs485;
short extref;
short rs422;
short rs232;
short ref_int;
short ref_ext;
short ref_auto;
short dummy1; //not used for anything, allows us to have an even amount of memory.
short dummy2; //not used for anything, allows us to have an even amount of memory.
short dummy3; //not used for anything, allows us to have an even amount of memory.
short dummy4; //not used for anything, allows us to have an even amount of memory.
short dummy5; //not used for anything, allows us to have an even amount of memory.
short dummy6; //not used for anything, allows us to have an even amount of memory.
short dummy7; //not used for anything, allows us to have an even amount of memory.
int chksum; //data checksum - equal to everything in the struct except this number
// mod 255.
};
struct data current_data; |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Thu Sep 06, 2007 2:42 am |
|
|
you can only use this syntax for bit fields: (There is an issue with byte boundaries, I think, you can't cross a byte with a bit field. Using 1 bit fields this does not concerns your code.)
Code: | struct data_record {
int a;
int b : 2; //2 bits
int c : 3; //3 bits
int d : 1; //1 bits
int d;
}data_var; |
|
|
|
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
|
Posted: Thu Sep 06, 2007 2:57 am |
|
|
libor, sorry you are wrong.
the error is caused by the naming
reducing current_data.Sequential_Shutter_Active_flag to
current_data.Seq_Shutter_Active_flag
if (current_data.Seq_Shutter_Active_flag==OFF) // not active
it now compiles |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Thu Sep 06, 2007 3:08 am |
|
|
OK. thanks for the info, so you just reached the variable name length limit. ..and I have been avoiding using shorts in structs in vain. |
|
|
|