Flag Word - Extracting Bits From Flag Words

Extracting Bits From Flag Words

To read a status byte, assuming your programming language does not offer this facility by default, is quite easy. You simply need to AND the status byte with a mask byte. The mask byte should have only the bit corresponding to the flag you want to read set, as in the example below.

Suppose that status byte 103 (decimal) is returned, and that we want to check flag bit 5.

The flag we want to read is number 5 (counting from zero) - so the mask byte will be . ANDing 32 with 103 gives 32, which means the flag bit is set. If the flag bit was not set, the result would have been 0.

In modern computing, the shift operator (<<) can be used to quickly perform the power-of-two. In general, a mask with the Nth bit set can be computed as

(1 << n)

Thus to check the Nth bit from a variable v, we can perform the operation

bool nth_is_set = (v & (1 << n)) != 0

Read more about this topic:  Flag Word

Famous quotes containing the words extracting, bits, flag and/or words:

    Life is a means of extracting fiction.
    Robert Stone (b. 1937)

    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)

    —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)

    There are words in that letter to his wife, respecting the education of his daughters, which deserve to be framed and hung over every mantelpiece in the land. Compare this earnest wisdom with that of Poor Richard.
    Henry David Thoreau (1817–1862)