You can use bitwise operators to do this (assuming you have few codes whose values ββremain in an integer variable).
a & (a - 1) returns a after canceling the last set bit. You can use this to get the value of the corresponding flag, for example:
while (QStatus) { uint nxtStatus = QStatus & (QStatus - 1); processFlag(QStatus ^ nxtStatus); QStatus = nxtStatus; }
processFlag will be called with the set values ββin ascending order (for example, 1, 4, 16, if QStatus initially 21).
source share