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)

    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)

    Columbus stood in his age as the pioneer of progress and enlightenment. The system of universal education is in our age the most prominent and salutary feature of the spirit of enlightenment, and it is peculiarly appropriate that the schools be made by the people the center of the day’s demonstration. Let the national flag float over every schoolhouse in the country and the exercises be such as shall impress upon our youth the patriotic duties of American citizenship.
    Benjamin Harrison (1833–1901)

    All my life I believed I knew something. But then one strange day came when I realized that I knew nothing, yes, I knew nothing. And so words became void of meaning ... I have arrived too late at ultimate uncertainty.
    Ezra Pound (1885–1972)