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)
“Assemble, first, all casual bits and scraps
That may shake down into a world perhaps;
People this world, by chance created so,
With random persons whom you do not know”
—Robert Graves (18951985)
“My dream is that as the years go by and the world knows more and more of America, it ... will turn to America for those moral inspirations that lie at the basis of all freedom ... that America will come into the full light of the day when all shall know that she puts human rights above all other rights, and that her flag is the flag not only of America but of humanity.”
—Woodrow Wilson (18561924)
“Euthanasia is a long, smooth-sounding word, and it conceals its danger as long, smooth words do, but the danger is there, nevertheless.”
—Pearl S. Buck (18921973)