Noob question about bit and byte

Status
Not open for further replies.

Manu

Well-known member
Hi,

I have a clue about doing tests on a bit in a byte

Code:
byte mot4 = frame.buf[4];
    Serial.print("buff 4 : "); Serial.println (mot4, BIN);
    if ((mot4 & 0x04) == 1) {
      resetCPU();
    }
    if ((mot4 & 0x08) == true) {      
      razconfModule();
      return;
    }

In this example, I try to look if bit 3 is set in byte "mot4" and do something. to do the test I use
Code:
if ((mot4 & 0x04) == 1)

But it doesn't operate.
Can someone point me to my problem ?

I know that something like
Code:
if ((mot4 >> 3) & 1U) == 1)
can work, but the first method should too ?

Thanks,
Manu
 
Alternatively, != 0 will also work for all these scenarios (or relying on the truthiness of non-zero integers: if (mot4 & 0x04) will also work).
 
Status
Not open for further replies.
Back
Top