locked
inspecting a bitmask RRS feed

  • Question

  • User379720387 posted

    bitmask: 0000011000010111

    I need to loop through a dataset where each member has a bit location value (say for this example I need to inspect locations 2, 7, 9).

    If that bit location is set (in the bitmask) I need to do something.

    Is there a spiffy way to do this, without converting the bitmask to a string? 

    Wednesday, May 8, 2019 9:25 PM

Answers

  • User475983607 posted
    Logical AND the two numbers. Use the shift command to find the high bits.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 8, 2019 10:15 PM
  • User379720387 posted

    Yes, bitwise AND i.e. "&".

    Ended up using enums set at the values of the positions I was interested in:

    enum3 = 4

    enum7 = 128

    enum9 = 512

    if ((bitmask & (int)SupplyMask.enum7) == (int)SupplyMask.enum7)
    {
       do stuff
    }

    Spiffy and readable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2019 3:37 PM

All replies

  • User475983607 posted
    Logical AND the two numbers. Use the shift command to find the high bits.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 8, 2019 10:15 PM
  • User379720387 posted

    Yes, bitwise AND i.e. "&".

    Ended up using enums set at the values of the positions I was interested in:

    enum3 = 4

    enum7 = 128

    enum9 = 512

    if ((bitmask & (int)SupplyMask.enum7) == (int)SupplyMask.enum7)
    {
       do stuff
    }

    Spiffy and readable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2019 3:37 PM