Last post May 09, 2019 03:37 PM by wavemaster
Contributor
2613 Points
2724 Posts
May 08, 2019 09:25 PM|wavemaster|LINK
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?
All-Star
53121 Points
23672 Posts
May 08, 2019 10:15 PM|mgebhard|LINK
May 09, 2019 03:37 PM|wavemaster|LINK
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.
Contributor
2613 Points
2724 Posts
inspecting a bitmask
May 08, 2019 09:25 PM|wavemaster|LINK
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?
All-Star
53121 Points
23672 Posts
Re: inspecting a bitmask
May 08, 2019 10:15 PM|mgebhard|LINK
Contributor
2613 Points
2724 Posts
Re: inspecting a bitmask
May 09, 2019 03:37 PM|wavemaster|LINK
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
Spiffy and readable.