Assign bitset member to char

Posted by RedX on Stack Overflow See other posts from Stack Overflow or by RedX
Published on 2012-07-03T15:12:13Z Indexed on 2012/07/03 15:15 UTC
Read the original article Hit count: 185

Filed under:

I have some code here that uses bitsets to store many 1 bit values into a char.

Basically

struct BITS_8 {
  char _1:1;
  (...)
  char _8:1;
}

Now i was trying to pass one of these bits as a parameter into a function

void func(char bit){
  if(bit){
    // do something
  }else{
    // do something else
}

// and the call was
struct BITS_8 bits;
// all bits were set to 0 before
bits._7 = 1;
bits._8 = 1;
func(bits._8);

The solution was to single the bit out when calling the function:

func(bits._8 & 0x128);

But i kept going into //do something because other bits were set. I was wondering if this is the correct behaviour or if my compiler is broken. The compiler is an embedded compiler that produces code for freescale ASICs.

© Stack Overflow or respective owner

Related posts about c