Go Down

Topic: [SOLVED] Bitwise and Arduino (Read 524 times) previous topic - next topic

TontonSancho

Oct 22, 2012, 10:39 am Last Edit: Oct 23, 2012, 07:49 am by TontonSancho Reason: 1
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

Nick Gammon

Post actual code, not pseudo code.

Read this before posting a programming question

Code: [Select]

sketch_oct22c:10: error: function definition does not declare parameters
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

TontonSancho

Expected actual result : "Never printed" will be never printed.
Code: [Select]

#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

Riva

Not sure what your trying to achieve but does writing the line like this help?
Code: [Select]
  if ((state & 0xF0) == GROUP_0 )

PeterH

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:
Code: [Select]

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



ETA: Riva beat me to it. :)

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.
I only provide help via the forum - please do not contact me for private consultancy.

Nick Gammon

The guys above are right. This works:

Code: [Select]

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:  http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

The == operator has higher precedence than the & operator.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

TontonSancho

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.

Go Up