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:

    “Anything in history or nature that can be described as changing steadily can be seen as heading toward catastrophe.”
    —Susan Sontag (b. 1933)

    “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 (1895–1985)

    “What is Americanism? Every one has a different answer. Some people say it is never to submit to the dictation of a King. Others say Americanism is the pride of liberty and the defence of an insult to the flag with their gore. When some half-developed person tramples on that flag, we should be ready to pour out the blood of the nation, they say. But do we not sit in silence when that flag waves over living conditions which should be an insult to all patriotism?”
    —Anna Howard Shaw (1847–1919)

    “Sprung from the West,
    He drank the valorous youth of a new world.
    The strength of virgin forests braced his mind,
    The hush of spacious prairies stilled his soul.
    His words were oaks in acorns; and his thoughts
    Were roots that firmly gript the granite truth.”
    —Edwin Markham (1852–1940)