Bitwise operator question

Status
Not open for further replies.

turtle9er

Well-known member
Hi,

I am working through some code and confused about what this means and how it works.
Code:
while(Serial2.available() > 0) {
c = Serial2.read();
buf[bufIndex++] = c;
Serial.println(c);

 // Process receive buffer if framing char received
    if(c & 0x80) {
Do processing in here}
}

What is the meaning of if (c & 0x80). I thought if the serial received was equal to 128, then process the buffer, however that does not seem to be the case.
This is an example of the values printed out that are received by the serial 4 64 0 15 127 124 248....and then it will process the data.

I have been reading up on bitwise operators, however I just can't understand what it means (guess I am stuck in logical thought). Any help would be great. Thanks.
 
Code:
(c & 0x80)
is a bitwise operation. If bit 7 in c is set, its result is nonzero. Using this expression in an if-statement means that if that bit is set, the "true" branch is executed. If c is an unsigned 8-bit number, the result is nonzero whenever c >= 128. If c is signed, this is equivalent to saying (c < 0) because the most significant bit is the sign.

Did that help?
 
Wow, thank you so much...that makes so much more sense that what I could find. And looking at the streaming of data I now see that there are never any values above 127, unless data is ready. Thank you so much.
 
Status
Not open for further replies.
Back
Top