Is there a method typically used to check if a flag is present in an int/other data type? I figured out something like this:
if ((host&flagtocheckfor)==flagtocheckfor)
Which works fine- however it’s such a common method of setting flags is this the way flags are usually checked? Or is there a more concise method?
That’s pretty well exactly the way bit flags are checked in most languages that support them.
For example:
While you can check something like the first with:
that won’t actually work for the second since it will return true if either of the bits are set, not both.
For completeness, C allows you to set the bit masks with:
You can clear them with:
And toggle them with: