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:
“Anything in history or nature that can be described as changing steadily can be seen as heading toward catastrophe.”
—Susan Sontag (b. 1933)
“Many bits of fox fur will make a fine robe.”
—Chinese proverb.
“Justice was done, and the President of the Immortals, in Æschylean phrase, had ended his sport with Tess. And the dUrberville knights and dames slept on in their tombs unknowing. The two speechless gazers bent themselves down to the earth, as if in prayer, and remained thus a long time, absolutely motionless: the flag continued to wave silently. As soon as they had strength they arose, joined hands again, and went on.
The End”
—Thomas Hardy (18401928)
“If thou canst believe, all things are possible to him that believeth.”
—Bible: New Testament Mark 9:23.
Jesus words to the father of a sick child.