Instructions
x86-64BT / BTS / BTR / BTC (bit test)
Test, set, reset or complement a single bit selected by index, capturing the old value in the carry flag. The compact way to manipulate bitfields and flags.
The bit-test family selects one bit of an operand by index and reports its
previous value in CF. They differ only in what they do
to that bit afterwards:
| Instr | Action on the selected bit | Mnemonic for |
|---|---|---|
BT | leave unchanged | test |
BTS | set to 1 | test-and-set |
BTR | clear to 0 | test-and-reset |
BTC | toggle | test-and-complement |
bt eax, 3 ; CF = bit 3 of eax (read)
bts eax, 3 ; CF = old bit 3; then set it
btr eax, 3 ; CF = old bit 3; then clear it
btc eax, 3 ; CF = old bit 3; then flip itThe bit index can be an immediate or a register, and the destination can be a register or memory.
A subtle memory addressing rule
When the destination is memory and the index is a register, the bit
offset is not masked to the operand size — the CPU computes
address + (index / 8) and indexes the bit within that byte. This lets bt
address a bit in an arbitrarily large in-memory bitmap with a single
instruction, but it also means bts [rax], rcx can touch memory far from
[rax]. Disassemblers and decompilers sometimes model this imprecisely, so
verify by hand when a bt* touches memory.
Reverse-engineering notes
- A
bt reg, immfollowed byjc/jnc(orsetc) is a clean "is flag N set?" test against a bitmask register — typical of permission flags, feature bits, and state machines. The immediate is the bit number. - Compilers often prefer the plain bitwise equivalents because they're faster on
most cores:
test eax, (1<<3)to test,or/and/xorwith a mask to set/clear/toggle. So a literalbt*frequently signals hand-written assembly, intrinsics (_bittest), or size-optimised code. bts/btron a memory operand with alockprefix is an atomic bit operation — the building block for spinlocks and lock-free bitsets. Spottinglock btspoints straight at concurrency code.
See also: AND / OR / NOT · Shift & rotate · BSF / BSR & population count · RFLAGS.