[SOLVED] Bitwise and Arduino

Hi all,

I tried to use bitwise operator without any success, here is my attempt:

#define GROUP_0 0x00
#define CHILD_0 0x01
#define CHILD_1 0x02

#define GROUP_1 0x10
#define CHILD_2 0x11
#define CHILD_3 0x12

int state;

some_function {
  state = GROUP_0;
  if ( state & 0xF0 == GROUP_0 ) return; // Always false

  state = CHILD_0;
  if ( state & 0xF0 == GROUP_0 ) return; // Always false

  state = CHILD_1;
  if ( state & 0xF0 == GROUP_0 ) return; // Always false
}

Am I missing something with bitwise ? with bitwise on arduino ?
Note: Same result if I use uint8_t for state variable.

Thanks in advance,
Sancho

Post actual code, not pseudo code.

Read this before posting a programming question

sketch_oct22c:10: error: function definition does not declare parameters

Expected actual result : "Never printed" will be never printed.

#include <Arduino.h>

#define GROUP_0 0x00
#define CHILD_0 0x01
#define CHILD_1 0x02

#define GROUP_1 0x10
#define CHILD_2 0x11
#define CHILD_3 0x12

int state;

void setup() {
  Serial.begin(9600);
}

void loop() {
  state = GROUP_0;
  if ( state & 0xF0 == GROUP_0 )
    Serial.println("Never printed for GROUP_0");

  state = CHILD_0;
  if ( state & 0xF0 == GROUP_0 )
    Serial.println("Never printed for CHILD_0");

  state = CHILD_1;
  if ( state & 0xF0 == GROUP_0 )
    Serial.println("Never printed for CHILD_1");
    
  delay(1000);
}

But I would expect tree lines every 1s ... isn't it ?

Regards

Not sure what your trying to achieve but does writing the line like this help?

  if ((state & 0xF0) == GROUP_0 )

At first glance the code looks reasonable, the basic logic looks OK.

I never like expressions with multiple different operators where the precedence is not defined explicitly. I think in this case the & and == are close together in precedence but not identical, and I think == has higher precedence. To apply the bitwise operator first, you need to define the precedence explicitly:

if ( (state & 0xF0) == GROUP_0 )

ETA: Riva beat me to it. :slight_smile:

I am not sure that will fix it, but it probably will. If it doesn't, then next thing to look at would be the types used in those expressions. To reduce the opportunity for the compiler to use the wrong type, I suggest you use 8 bit integers throughout (whichever type you prefer - uint8_t would be fine, and replace your #define macros with const variables so that all your constants are correctly typed, too.

The guys above are right. This works:

void loop() {
  state = GROUP_0;
  if ( (state & 0xF0) == GROUP_0 )
    Serial.println("Never printed for GROUP_0");

  state = CHILD_0;
  if ( (state & 0xF0) == GROUP_0 )
    Serial.println("Never printed for CHILD_0");

  state = CHILD_1;
  if ( (state & 0xF0) == GROUP_0 )
    Serial.println("Never printed for CHILD_1");
    
  delay(1000);
}

See: Operators in C and C++ - Wikipedia

The == operator has higher precedence than the & operator.

Oh, you are all totally right.

What a pain to believe priority of bitwise operators is the same as math operator (+ - * /).

Now I will be careful of these & and | ...

Thank's a lot all.