Changing Bits in Flag Words
Writing, reading or toggling bits in flags can be done only using the OR, AND and NOT operations - operations which can be performed quickly in the processor.
To set a bit, OR the status byte with a mask byte. Any bits set in the mask byte or the status byte will be set in the result.
int setBit(int val, int bit_position) { return val | (1 << bit_position); }To clear a bit, perform a NOT operation on the mask byte, then AND it with the status byte. The result will have the appropriate flag cleared (set to 0).
int clearBit(int val, int bit_position) { return val & ~(1 << bit_position); }To toggle a bit, XOR the status byte and the mask byte. This will set a bit if it is cleared or clear a bit if it is set.
int toggleBit(int val, int bit_position) { return val ^ (1 << bit_position); }Read more about this topic: Flag Word
Famous quotes containing the words changing, bits, flag and/or words:
“Ones condition on marijuana is always existential. One can feel the importance of each moment and how it is changing one. One feels ones being, one becomes aware of the enormous apparatus of nothingnessthe hum of a hi-fi set, the emptiness of a pointless interruption, one becomes aware of the war between each of us, how the nothingness in each of us seeks to attack the being of others, how our being in turn is attacked by the nothingness in others.”
—Norman Mailer (b. 1923)
“Many bits of fox fur will make a fine robe.”
—Chinese proverb.
“Our flag is red, white and blue, but our nation is a rainbowred, yellow, brown, black and whiteand were all precious in Gods sight.”
—Jesse Jackson (b. 1941)
“The words which express our faith and piety are not definite; yet they are significant and fragrant like frankincense to superior natures.”
—Henry David Thoreau (18171862)