testing bitvariable structures

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,
I make a fair bit of use of bitflags in my code
eg.

struct 
{ 
     unsigned char bit0 : 1; 
     unsigned char bit1 : 1; 
      ... 
      unsigned char bit7 : 7; 
}bitbyte1,bitbyte2;

Sometimes I wish to know if the flags are ALL CLEAR to save time.
I have tried the following

if(bitbyte1!=0)
   ;test which bit is set else do no tests at all

,but it throws up a compiler error

error: invalid operands to binary == (have 'volatile struct ' and 'int')

I have not come up with an elegant way of doing this!.
Any thoughts on how I can avoid a compound if statement.
[/code]

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
if (*(unsigned char*)&bitbyte1 != 0)

(Assuming that the '7' is a typo)

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use a union, and read the member you require.

It all looks quite nice in source code. But it is worth just checking the generated code every now and again. You may get a shock.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,
i was trying your code and pointers should help:

unsigned char * pbitbyte1 = &bitbyte1;
unsigned char * pbitbyte2 = &bitbyte2;

But i don't tested much deeply.

Computers don't make errors - What they do they do on purpose.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wow that was quick, just had dinner in between post & getting answer.
I will give both ideas a try.
Thanks for the tip on code size Dave. I have seen reference to code bloat when using structures, but have not sussed it out.
Thanks gents!

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

IMHO pointer magic is way too often (ab)used in C. I prefer to use the union as David suggests.

You can do even more, e.g. checking for any number of consecutive bit at once. Something you would do with a mask on a byte/word, but again this might express your intention more cleanly.

David wrote:
It all looks quite nice in source code. But it is worth just checking the generated code every now and again. You may get a shock.

Yeah, maybe - but, again, once you use HLL, you should stop to be bothered too much. If you care about speed/size, use asm.

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

IMHO pointer magic is way too often (ab)used in C.

Isn't that exactly why us frustrated Asm programmers choose C in the first place? ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I don't quite see how the *frustration* of asm programmer makes him NOT to choose a real high-level language.

I thought, this is simply because the real stuff (a.k.a. "boring" among the cool'a'freak-s) does not get promoted in such an aggressive, viral way... ;-)

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

At the price of another level during "normal" use, a union can work well for setting/testing "banks" of bit variables.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
At the price of another level during "normal" use

Not necessarily. gcc supports anonymous structs.

union {
  struct {
    uint8_t b0 : 1;
    uint8_t b1 : 1;
    uint8_t b2 : 1;
    uint8_t b3 : 1;
    uint8_t b4 : 1;
    uint8_t b5 : 1;
    uint8_t b6 : 1;
    uint8_t b7 : 1;
  };
  struct {
    uint8_t    : 3;  // dummy spacefiller for b0-b2
    uint8_t bits3through5 : 3;
  };
  uint8_t b;
} myVariable;

myVariable.b0 = 1;
myVariable.bits3through5 = 4;

etc.

JW

PS. http://gcc.gnu.org/onlinedocs/gc...