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:
“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 (16671745)
“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)
“Justice was done, and the President of the Immortals, in Æschylean phrase, had ended his sport with Tess. And the dUrberville knights and dames slept on in their tombs unknowing. The two speechless gazers bent themselves down to the earth, as if in prayer, and remained thus a long time, absolutely motionless: the flag continued to wave silently. As soon as they had strength they arose, joined hands again, and went on.
The End”
—Thomas Hardy (18401928)
“Good as is discourse, silence is better, and shames it. The length of the discourse indicates the distance of thought betwixt the speaker and the hearer. If they were at a perfect understanding in any part, no words would be necessary thereon. If at one in all parts, no words would be suffered.”
—Ralph Waldo Emerson (18031882)