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)

    What in fact have I achieved, however much it may seem? Bits and pieces ... trivialities. But here they won’t tolerate anything else, or anything more. If I wanted to take one step in advance of the current views and opinions of the day, that would put paid to any power I have. Do you know what we are ... those of us who count as pillars of society? We are society’s tools, neither more nor less.
    Henrik Ibsen (1828–1906)

    Up rose old Barbara Frietchie then,
    Bowed with her fourscore years and ten;

    Bravest of all in Frederick town,
    She took up the flag the men hauled down;
    John Greenleaf Whittier (1807–1892)

    I am not of the opinion generally entertained in this country [England], that man lives by Greek and Latin alone; that is, by knowing a great many words of two dead languages, which nobody living knows perfectly, and which are of no use in the common intercourse of life. Useful knowledge, in my opinion, consists of modern languages, history, and geography; some Latin may be thrown into the bargain, in compliance with custom, and for closet amusement.
    Philip Dormer Stanhope, 4th Earl Chesterfield (1694–1773)