Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Noob question about bit and byte

  1. #1

    Noob question about bit and byte

    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

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,006
    Code:
    if ((mot4 & 0x04) == 0x04)
    should work

  3. #3
    Thank, it does

  4. #4
    Junior Member
    Join Date
    Jan 2019
    Posts
    18
    Alternatively, != 0 will also work for all these scenarios (or relying on the truthiness of non-zero integers: if (mot4 & 0x04) will also work).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •