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:

    Minnaloushe creeps through the grass
    Alone, important and wise,
    And lifts to the changing moon
    His changing eyes.
    William Butler Yeats (1865–1939)

    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)

    There’s an enduring American compulsion to be on the side of the angels. Expediency alone has never been an adequate American reason for doing anything. When actions are judged, they go before the bar of God, where Mom and the Flag closely flank His presence.
    Jonathan Raban (b. 1942)

    Overworked assonance, nonsense,
    juxtaposition of words for words’ sake
    without meaning, undefined; imposition,
    deception, indecisive weather-vane;
    disagreeable, inconsequent syllables,
    too malleable, too brittle.
    Hilda Doolittle (1886–1961)