How do you set specific bits on a binary number in IA32 or x86 ISA?
I've used an开发者_StackOverflow社区other processor that had something like BIS to the same thing.
In addition to the normal AND
and OR
, there are also BTS
(set), BTC
(complement), and BTR
(reset) that do test-and-X operations on single bits (plus BT
which is just the test).
You can use the OR
instructions to set specific bits with a bit pattern and AND
to clear them (using the one's complement of the bit pattern you would use to set them). The bit patterns can be adjusted to set and clear multiple bits at a time.
For example, let's say you're dealing with 8-bit values and you want to set bits 4 and 3 (x
means it could be either 0
or 1
):
xxxx xxxx
OR 0001 1000
= xxx1 1xxx
Then, to clear bits 7, 6, 2 and 0:
xxx1 1xxx
AND 0011 1010
= 00x1 10x0
OR
and AND
should be supported on just about every CPU out there. I can't guarantee that but I've never seen one without it (and I've seen quite a few).
精彩评论