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:
“Peoples affections can be as thin as paper; life is like a game of chess, changing with each move.”
—Chinese proverb.
“You have to begin to lose your memory, if only in bits and pieces, to realize that memory is what makes our lives. Life without memory is no life at all, just as an intelligence without the possibility of expression is not really an intelligence. Our memory is our coherence, our reason, our feeling, even our action. Without it, we are nothing.”
—Luis Buñuel (19001983)
“By the rude bridge that arched the flood,
Their flag to Aprils breeze unfurled,
Here once the embattled farmers stood
And fired the shot heard round the world.”
—Ralph Waldo Emerson (18031882)
“Out of the slimy mud of words, out of the sleet and hail of verbal imprecisions,
Approximate thoughts and feelings, words that have taken the place of thoughts and feelings,
There springs the perfect order of speech, and the beauty of incantation.”
—T.S. (Thomas Stearns)