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:
“Love is the most melodious of all the harmonies, and we have an innate feeling for it. Woman is a delicious instrument of pleasure, but one must know the chords, study the pose of it, the timid keyboard, the changing and capricious fingering.”
—HonorĂ© De Balzac (17991850)
“Assemble, first, all casual bits and scraps
That may shake down into a world perhaps;
People this world, by chance created so,
With random persons whom you do not know”
—Robert Graves (18951985)
“Hath not the morning dawned with added light?
And shall not evening call another star
Out of the infinite regions of the night,
To mark this day in Heaven? At last, we are
A nation among nations; and the world
Shall soon behold in many a distant port
Another flag unfurled!”
—Henry Timrod (18281867)
“The word infant derives from Latin words meaning not yet speaking. It emphasizes what the child cannot do and reflects the babys total dependence on adults. The word toddler, however, demonstrates our change in perspective, for it focuses on the childs increased mobility and burgeoning independence.”
—Lawrence Kutner (20th century)