I'm currently working on a project where I read a 32 bit sequence. The digitalPin is always HIGH when no sequence is received and starts oscilating with about 1kHz when the sequence starts (RC5).
I am setting and clearing bits of an uint32_t depending on which signal is on the digitalPin. It seems to work very well with every bit except of the MSB.
Here a short code snippet to show what I mean:
const short irPin = 8;
const short msgDelay = 250;
uint32_t data = 0xFFFFFFFF;
long msgArrived = 0;
void setup(){
Serial.begin(115200);
pinMode(irPin, INPUT);
}
void loop(){
long now = millis();
if( (!(PINB & 1)) && ((now - msgArrived)> msgDelay)){
msgArrived = millis();
bitClear(data, 31);
Serial.println(data, BIN);
}
}
This gives me an output of
11111111111111111111111111111111
I have already tried
bitClear(data, 32); -> 11111111111111111111111111111111
bitClear(data, 30); -> 10111111111111111111111111111111
So every output is right, but not the when I try to clear the msb.
Has anyone experienced a similar behaviour?
1 Answer 1
uint32_t data = 0xFFFFFFFF;
Serial.println(data, BIN);
data = 0xFFFFFFFF;
bitClear(data, 30);
Serial.println(data, BIN);
data = 0xFFFFFFFF;
bitClear(data, 31);
Serial.println(data, BIN);
On my mega outputs:
11111111111111111111111111111111
10111111111111111111111111111111
1111111111111111111111111111111
Are you sure you aren't mistaking the fact that println doesn't print leading 0's for the number being nothing but 1's? Try printing as hex, then all the numbers will be the same width:
FFFFFFFF
BFFFFFFF
7FFFFFFF
-
2I fact i actually did miss out the fact, that println doesn't print leading zeros. Thanks. Would +1, but I am missing reputation for that.elhe– elhe2016年06月17日 16:51:52 +00:00Commented Jun 17, 2016 at 16:51
data &= ~(0x1 << 31)
?