Hi
Heres the result of a little test i made of bit manipulation on a Nano (mega168), as i didn't understand how it worked. hope i will help somebody.
Observations:
- _BV(24) and (1<< 24) dosn't work with unsigned Long
- it seems that bit(2) , _BV(2) , (1<< 2) set AND reset all bits - bitSet (d, 2) and e |= bit (2) set ONLY 1 bit
- _BV(15) and (1<< 15) gives odd results.....
void setup () {
Serial.begin (115200);
unsigned long a, b, c, d, e;
a = 0, b = 0, c = 0, d = 0, e = 0;
Serial.println("unsigned long a, b, c, d, e;\r\n");
Serial.print ("agument"); Serial.print ("\t\t"); Serial.print ("DEC"); Serial.print ("\t\t"); Serial.println ("BIN");
Serial.print("a = bit(2) "); a = bit (2); Serial.print ("\t"); Serial.print (a); Serial.print ("\t\t"); Serial.println (a, BIN);
Serial.print("b = _BV(2) "); b = _BV (2); Serial.print ("\t"); Serial.print (b); Serial.print ("\t\t"); Serial.println (b, BIN);
Serial.print("c = (1<< 2) "); c = (1 << 2); Serial.print ("\t"); Serial.print (c); Serial.print ("\t\t"); Serial.println (c, BIN);
Serial.print("bitSet (d, 2) "); bitSet (d, 2); Serial.print ("\t"); Serial.print (d); Serial.print ("\t\t"); Serial.println (d, BIN);
Serial.print("e |= bit (2) "); e |= bit (2); Serial.print ("\t"); Serial.print (e); Serial.print ("\t\t"); Serial.println (e, BIN);
Serial.println("######### Reset #########");
a = 0, b = 0, c = 0, d = 0, e = 0;
Serial.print("a = bit(24) "); a = bit (24); Serial.print ("\t"); Serial.print (a); Serial.print ("\t"); Serial.println (a, BIN);
Serial.print("b = _BV(24) "); b = _BV (24); Serial.print ("\t"); Serial.print (b); Serial.print ("\t\t"); Serial.println (b, BIN);
Serial.print("c = (1<< 24) "); c = (1 << 24); Serial.print ("\t"); Serial.print (c); Serial.print ("\t\t"); Serial.println (c, BIN);
Serial.print("bitSet (d, 24) "); bitSet (d, 24); Serial.print ("\t"); Serial.print (d); Serial.print ("\t"); Serial.println (d, BIN);
Serial.print("e |= bit (24) "); e |= bit (24); Serial.print ("\t"); Serial.print (e); Serial.print ("\t"); Serial.println (e, BIN);
Serial.println("######### Reset #########");
a = 0, b = 0, c = 0, d = 0, e = 0;
Serial.print("a = bit(15) "); a = bit (15); Serial.print ("\t"); Serial.print (a); Serial.print ("\t\t"); Serial.println (a, BIN);
Serial.print("b = _BV(15) "); b = _BV (15); Serial.print ("\t"); Serial.print (b); Serial.print ("\t"); Serial.println (b, BIN);
Serial.print("c = (1<< 15) "); c = (1 << 15); Serial.print ("\t"); Serial.print (c); Serial.print ("\t"); Serial.println (c, BIN);
Serial.print("bitSet (d, 15) "); bitSet (d, 15); Serial.print ("\t"); Serial.print (d); Serial.print ("\t\t"); Serial.println (d, BIN);
Serial.print("e |= bit (15) "); e |= bit (15); Serial.print ("\t"); Serial.print (e); Serial.print ("\t\t"); Serial.println (e, BIN);
Serial.println("********* NO Reset ********");
// a = 0, b = 0, c = 0, d = 0, e = 0;
Serial.print("a = bit(2) "); a = bit (2); Serial.print ("\t"); Serial.print (a); Serial.print ("\t\t"); Serial.println (a, BIN);
Serial.print("b = _BV(2) "); b = _BV (2); Serial.print ("\t"); Serial.print (b); Serial.print ("\t\t"); Serial.println (b, BIN);
Serial.print("c = (1<< 2) "); c = (1 << 2); Serial.print ("\t"); Serial.print (c); Serial.print ("\t\t"); Serial.println (c, BIN);
Serial.print("bitSet (d, 2) "); bitSet (d, 2); Serial.print ("\t"); Serial.print (d); Serial.print ("\t\t"); Serial.println (d, BIN);
Serial.print("e |= bit (2) "); e |= bit (2); Serial.print ("\t"); Serial.print (e); Serial.print ("\t\t"); Serial.println (e, BIN);
Serial.println("######### Reset #########");
a = 0, b = 0, c = 0, d = 0, e = 0;
Serial.print("a = bit(2) "); a = bit (2); Serial.print ("\t"); Serial.print (a); Serial.print ("\t\t"); Serial.println (a, BIN);
Serial.print("b = _BV(2) "); b = _BV (2); Serial.print ("\t"); Serial.print (b); Serial.print ("\t\t"); Serial.println (b, BIN);
Serial.print("c = (1<< 2) "); c = (1 << 2); Serial.print ("\t"); Serial.print (c); Serial.print ("\t\t"); Serial.println (c, BIN);
Serial.print("bitSet (d, 2) "); bitSet (d, 2); Serial.print ("\t"); Serial.print (d); Serial.print ("\t\t"); Serial.println (d, BIN);
Serial.print("e |= bit (2) "); e |= bit (2); Serial.print ("\t"); Serial.print (e); Serial.print ("\t\t"); Serial.println (e, BIN);
} // end of setup
void loop () { }
Output:
unsigned long a, b, c, d, e;
agument DEC BIN
a = bit(2) 4 100
b = _BV(2) 4 100
c = (1<< 2) 4 100
bitSet (d, 2) 4 100
e |= bit (2) 4 100
######### Reset #########
a = bit(24) 16777216 1000000000000000000000000
b = _BV(24) 0 0
c = (1<< 24) 0 0
bitSet (d, 24) 16777216 1000000000000000000000000
e |= bit (24) 16777216 1000000000000000000000000
######### Reset #########
a = bit(15) 32768 1000000000000000
b = _BV(15) 4294934528 11111111111111111000000000000000
c = (1<< 15) 4294934528 11111111111111111000000000000000
bitSet (d, 15) 32768 1000000000000000
e |= bit (15) 32768 1000000000000000
********* NO Reset ********
a = bit(2) 4 100
b = _BV(2) 4 100
c = (1<< 2) 4 100
bitSet (d, 2) 32772 1000000000000100
e |= bit (2) 32772 1000000000000100
######### Reset #########
a = bit(2) 4 100
b = _BV(2) 4 100
c = (1<< 2) 4 100
bitSet (d, 2) 4 100
e |= bit (2) 4 100