Instruction
BTR
Copies the selected bit of a register or memory operand into CF and then clears it; with LOCK the memory form is atomic.
- Extensions
- Forms
- 6 (6 Legacy)
- Data
- Intel XED v2026.08.23
BTR copies one bit of its first operand into CF and then clears that bit. It is BTS with the opposite write: the bit is chosen as BT chooses it, ZF stays as it was, and with LOCK on a memory operand the read, the clear and the write back are one atomic operation. CF then says whether this BTR was the one that cleared the bit, which makes it a test-and-clear: a thread can claim a slot marked free by a set bit, or consume a pending-event bit, and know that no other thread got the same one.
From C
GCC 14 and Clang 23 at -O2 compile
__atomic_fetch_and(p, ~(1UL << 40), __ATOMIC_SEQ_CST) & (1UL << 40)
to lock btr qword ptr [rdi], 40 and a SETC (Clang prints it setb),
and the same with a bit number n in a register to and esi, 63 and
lock btr qword ptr [rdi], rsi: the AND stays because a memory operand
does not take the offset modulo 64. Without the test of the old bit,
both emit lock and with a mask instead.
On a register, x & ~(1UL << n) becomes btr rax, rsi. For a
constant bit above 31, such as 40 or the sign bit 63, GCC writes
btr rax, 40, while Clang loads the inverted mask with a 10-byte
MOVABS and ANDs it, since an AND immediate holds at most 32 bits,
sign-extended.
Clearing a bit in memory
Neither compiler uses BTR on plain memory. For
a[n / 64] &= ~(1UL << (n % 64)), GCC builds the mask with a shift and
a NOT, and Clang with mov rax, -2 and rol rax, cl, rotating the one
zero into place, before an AND to memory. The instruction table of
AMD’s Zen 5 guide lists every memory form of BTR as microcoded and the
register forms as 2 macro-ops.
Things to know
- Not the lowest bit. To clear the lowest set bit, whichever it is, BLSR does it without a bit number.
- Undefined flags. OF, SF, AF and PF are undefined; Zen 5 left them as they were in all 168 recorded cases.
Forms
| Instruction | Encoding | Requires |
|---|---|---|
BTR m16/32/64, imm8
BTR_MEMv_IMMb | 0F BA /6 ib | Base instruction set |
BTR r16/32/64, imm8
BTR_GPRv_IMMb | 0F BA /6 ib | Base instruction set |
BTR m16/32/64, r16/32/64
BTR_MEMv_GPRv | 0F B3 /r | Base instruction set |
BTR r16/32/64, r16/32/64
BTR_GPRv_GPRv | 0F B3 /r | Base instruction set |
LOCK BTR m16/32/64, imm8
BTR_LOCK_MEMv_IMMb | 0F BA /6 ib | Base instruction set |
LOCK BTR m16/32/64, r16/32/64
BTR_LOCK_MEMv_GPRv | 0F B3 /r | Base instruction set |
Latency and throughput
| Model | m16, imm8m32, imm8m64, imm8BTR m16, imm8BTR m32, imm8BTR m64, imm8 | r16, imm8r32, imm8r64, imm8r16, r16r32, r32 | m16, r16m32, r32BTR m16, r16BTR m32, r32 | m64, r64BTR m64, r64 | r64, r64 |
|---|---|---|---|---|---|
| Sandy Bridge | 7 / 1p05 | 1 / 0.5p05 | 9 / 1p015 | 9 / 1p015 | 1 / 0.5p05 |
| Haswell | 7 / 1p06 | 1 / 0.5p06 | 1 / 2.75 | 1 / 2.75 | 1 / 0.5p06 |
| Broadwell, Skylake, Skylake-SP | 6 / 1p06 | 1 / 0.5p06 | 6 / 1p0156 | 6 / 1p0156 | 1 / 0.5p06 |
| Ice Lake | 6 / 0.5p06 | 1 / 0.5p06 | 6 / 0.5p0156 | 6 / 0.5p0156 | 1 / 0.5p06 |
| Alder Lake, Sapphire Rapids | 12 / 1p1 | 1 / 1p1 | 18 / 1.6p0 p1 p5 p6 p11 | 17 / 1.4p0 p1 p5 p6 p11 | 3 / 1p1 |
| Lunar Lake | 12 / 0.5p135 | 1 / 0.33p135 | 18 / 1.33p012345 | 17 / 1.17p012345 | 3 / 1p1 |
| Bonnell | 2 / 1p1 | 1 / 1p1 | 11 / 5.5p1 | 11 / 5.5p1 | 1 / 1p1 |
| Silvermont | 4 / 2IEC0 IEC1 MEC | 1 / 1IEC0 IEC1 | 4 / 2IEC0 IEC1 MEC | 4 / 2IEC0 IEC1 MEC | 1 / 1IEC0 IEC1 |
| Piledriver | 7 / 21EX0 EX1 | 2 / 1EX0 EX1 | 7 / 22EX0 EX1 | 7 / 22EX0 EX1 | 2 / 1EX0 EX1 |
| Zen | 6 / 0.5ALU0 ALU1 ALU2 ALU3 | 2 / 0.25ALU0 ALU1 ALU2 ALU3 | 6 / 0.5ALU0 ALU1 ALU2 ALU3 | 6 / 0.5ALU0 ALU1 ALU2 ALU3 | 2 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Zen 2 | 6 / 0.33ALU0 ALU1 ALU2 ALU3 | 2 / 0.25ALU0 ALU1 ALU2 ALU3 | 6 / 0.33ALU0 ALU1 ALU2 ALU3 | 6 / 0.33ALU0 ALU1 ALU2 ALU3 | 2 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Zen 3, Zen 4 | 7 / 0.67ALU1 ALU2 | 2 / 1ALU1 ALU2 | 7 / 0.67ALU1 ALU2 | 7 / 0.67ALU1 ALU2 | 2 / 1ALU1 ALU2 |
| Jaguar | 6 / 1ALU0 ALU1 | 1 / 0.5ALU0 ALU1 | 6 / 1ALU0 ALU1 | 6 / 1ALU0 ALU1 | 1 / 0.5ALU0 ALU1 |
Operation
What the forms do, in pseudocode that our tests run against results recorded on real processors. How to read.
BTR r/m16/32/64, imm8 BTR r16/32/64, r16/32/64 LOCK BTR m16/32/64, imm8n = bit & (OSIZE - 1) # the offset, modulo the operand size
v = dest.read() # with LOCK: one atomic read-modify-write
cf = v >> n & 1
of = sf = af = pf = UNDEFINED # ZF keeps its value
dest.write(v & ~(1 << n)) # a 32-bit register: bits 63:32 cleared
Run on AMD Zen 5 (EPYC 9645): 99 cases.
BTR m16/32/64, r16/32/64 LOCK BTR m16/32/64, r16/32/64n = signed(bit) # not masked: bit n can lie far below
# or above the operand
a = dest.address + n // OSIZE * (OSIZE // 8) # the OSIZE bits that
v = load(a, OSIZE) # hold it; with LOCK,
cf = v >> n % OSIZE & 1 # read and written as one
of = sf = af = pf = UNDEFINED # ZF keeps its value
store(a, OSIZE, v & ~(1 << n % OSIZE))Run on AMD Zen 5 (EPYC 9645): 69 cases.
Operands
| Operand | Kind | Action |
|---|---|---|
BTR r/m16/32/64, imm8 LOCK BTR m16/32/64, imm8 | ||
r/m16/32/64 | explicit | read, write |
imm8 | explicit | read |
RFLAGS | implicit | write |
BTR r/m16/32/64, r16/32/64 LOCK BTR m16/32/64, r16/32/64 | ||
r/m16/32/64 | explicit | read, write |
r16/32/64 | explicit | read |
RFLAGS | implicit | write |
Flags
| OF | SF | AF | PF | CF |
|---|---|---|---|---|
| All forms | ||||
| U | U | U | U | M |
Attributes
What else XED records about the forms. How to read.
- Memory forms without LOCK
- accepts a LOCK prefix
- LOCK forms
- locked: atomic read-modify-write; accepts XACQUIRE; accepts XRELEASE
CPUID and processors
Each form belongs to an XED ISA set. A form can be used when the processor reports every CPUID bit of one of its ISA set's alternatives.
| ISA set | CPUID | Processors |
|---|---|---|
I386 | No CPUID bit: part of the base instruction set. | Every x86-64 processor in XED’s model and Zen 3 to Zen 6 |
More General-purpose instructions
Sources
- Intel XED v2026.08.23 (commit
0bcb6237345c): forms, encodings, operands, flags, exception classes, attributes, ISA sets, CPUID bits and chip model. - The CPU data of x86-64.net: Zen 3 to Zen 6, from GCC’s
-marchfeature sets and AMD’s Processor Programming References, which the page of each processor cites. - LLVM 23.1.2's x86 scheduling models (commit
85ac56026243): the latency, reciprocal throughput and ports of each form, as llvm-mca reports them. - Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 2 (325383-093, September 2026): Volume 2A, page 3-116 (BTR)
- AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and System Instructions (24594, rev. 3.39, October 2026)
- Software Optimization Guide for the AMD Zen5 Microarchitecture (58455, rev. 1.00, August 2024), Wayback Machine copy of AMD's ZIP
The tables are derived from Intel XED, Copyright Intel Corporation, licensed under the Apache License 2.0; x86-64.net converted and reformatted them. The timings come from LLVM's scheduling models, part of the LLVM Project, licensed under the Apache License 2.0 with LLVM Exceptions. The text is our own.