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)) != 0Read 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)
“You have to begin to lose your memory, if only in bits and pieces, to realize that memory is what makes our lives. Life without memory is no life at all, just as an intelligence without the possibility of expression is not really an intelligence. Our memory is our coherence, our reason, our feeling, even our action. Without it, we are nothing.”
—Luis Buñuel (19001983)
“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 (18071892)
“The Marxist vision of man without God must eventually be seen as an empty and a false faiththe second oldest in the worldfirst proclaimed in the Garden of Eden with whispered words of temptation: Ye shall be as gods.”
—Ronald Reagan (b. 1911)