Flag Word - Changing Bits in Flag Words

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:

    A love affair should always be a honeymoon. And the only way to make sure of that is to keep changing the man; for the same man can never keep it up.
    George Bernard Shaw (1856–1950)

    They’re semiotic phantoms, bits of deep cultural imagery that have split off and taken on a life of their own, like those Jules Verne airships that those old Kansas farmers were always seeing.... Semiotic ghosts. Fragments of the Mass Dream, whirling past in the wind of my passage.
    William Gibson (b. 1948)

    Our flag is red, white and blue, but our nation is a rainbow—red, yellow, brown, black and white—and we’re all precious in God’s sight.
    Jesse Jackson (b. 1941)

    Formerly, when lying awake at midnight in those woods, I had listened to hear some words or syllables of their language, but it chanced that I listened in vain until I heard the cry of the loon. I have heard it occasionally on the ponds of my native town, but there its wildness is not enhanced by the surrounding scenery.
    Henry David Thoreau (1817–1862)