This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Why does this particular flag involve an "or" operation?

uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED |
                BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;

And then when I look up "BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;", I got this:

(BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE | BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED)   /**< LE General Discoverable Mode, BR/EDR not supported. */

I'm having a bit of brain stall right now and can't figure it out. I know it's a trick to make the whole thing a "switch" of some sort but really don't know how.

Parents
  • The OR operator is being used to set multiple bits of that flag in a single line.

    For example, conside you have a flag of 1 byte (8 bits), and each bit controls one feature.

    #define FEATURE_ONE   0b00000001
    #define FEATURE_TWO   0b00000010
    #define FEATURE_THREE 0b00000100
    #define FEATURE_FOUR  0b00001000
    #define FEATURE_FIVE  0b00010000
    #define FEATURE_SIX   0b00100000
    #define FEATURE_SEVEN 0b01000000
    #define FEATURE_EIGHT 0b10000000
    

    And now you wish to turn on features 2 and 5. You can do so with

    uint8_t flags = FEATURE_TWO | FEATURE_FIVE;
    

    This works because when doing an OR of the two numbers, the set bits from each number prevail, so:

        00000010
        00010000
    or__________
        00010010
    
  • Please accept one of the answers, to show the case as closed.

Reply Children
No Data
Related