Pages: [1]   Go Down
Author Topic: [SOLVED] Bitwise and Arduino  (Read 402 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: October 23, 2012, 12:49:11 am by TontonSancho » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post actual code, not pseudo code.

Read this before posting a programming question

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Expected actual result : "Never printed" will be never printed.
Code:
#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
Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Handle every stressful situation like a dog. If you can't eat it or hump it. Piss on it and walk away.

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11130
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if ( (state & 0xF0) == GROUP_0 )


ETA: Riva beat me to it. smiley

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The guys above are right. This works:

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: