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:

    He had been eight years upon a project for extracting sunbeams out of cucumbers, which were to be put into vials hermetically sealed, and let out to warm the air in raw, inclement summers.
    Jonathan Swift (1667–1745)

    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)

    Public opinion contains all kinds of falsity and truth, but it takes a great man to find the truth in it. The great man of the age is the one who can put into words the will of his age, tell his age what its will is, and accomplish it. What he does is the heart and the essence of his age, he actualizes his age. The man who lacks sense enough to despise public opinion expressed in gossip will never do anything great.
    Georg Wilhelm Friedrich Hegel (1770–1831)