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:

    “Every political system is an accumulation of habits, customs, prejudices, and principles that have survived a long process of trial and error and of ceaseless response to changing circumstances. If the system works well on the whole, it is a lucky accident—the luckiest, indeed, that can befall a society.”
    —Edward C. Banfield (b. 1916)

    “On their slag heap, these children
    Wear skins peeped through by bones and spectacles of steel
    With mended glass, like bottle bits in slag.”
    —Stephen Spender (1909–1995)

    “—Here, the flag snaps in the glare and silence
    Of the unbroken ice. I stand here,
    The dogs bark, my beard is black, and I stare
    At the North Pole. . .
    And now what? Why, go back.

    Turn as I please, my step is to the south.”
    —Randall Jarrell (1914–1965)

    “This unlettered man’s speaking and writing are standard English. Some words and phrases deemed vulgarisms and Americanisms before, he has made standard American; such as “It will pay.” It suggests that the one great rule of composition—and if I were a professor of rhetoric I should insist on this—is, to speak the truth. This first, this second, this third; pebbles in your mouth or not. This demands earnestness and manhood chiefly.”
    —Henry David Thoreau (1817–1862)