Instruction
BTS
Copies the selected bit of a register or memory operand into CF and then sets it; with LOCK the memory form is an atomic test-and-set.
- Extensions
- Forms
- 6 (6 Legacy)
- Data
- Intel XED v2026.08.23
BTS copies one bit of its first operand into CF and then sets that bit to 1. It selects the bit as BT does, so a register offset on a memory operand can reach far outside the addressed word, and it leaves ZF alone. BTR clears the bit instead and BTC flips it.
Test-and-set
With LOCK and a memory operand, BTS reads, sets and writes back as one
atomic operation, and CF tells whether the bit was already set: a
one-bit test-and-set, for a bitmap of free slots or a flag that only one
thread may claim. GCC 14 and Clang 23 at -O2 compile
__atomic_fetch_or(p, 1UL << n, __ATOMIC_SEQ_CST) & (1UL << n) to
lock bts qword ptr [rdi], rsi and a SETC (Clang prints it setb),
and a constant bit 5 to lock bts qword ptr [rdi], 5. When the old
value is not used at all, they emit lock or instead.
The mask the compilers keep
Written as 1UL << (n & 63), that call still gets an and esi, 63
before the LOCK BTS from both compilers, while x | (1UL << (n & 63))
on a register becomes a bare bts rax, rsi. A register operand takes
the offset modulo 64 by itself; a memory operand does not, and an
unmasked RSI would set a bit in another word of memory. In Zen 5’s
cases, an offset of -512 set bit 0 of the byte 64 bytes below the
address, and LOCK BTS with 511 set bit 7 of the byte 63 bytes above it.
On registers, GCC also sets a constant bit such as 40 with
bts rax, 40, where Clang loads the mask with a 10-byte MOVABS and ORs
it in.
Things to know
- Speed. The instruction table of AMD’s Zen 5 guide lists the
register forms as 2 macro-ops and every memory form, with an
immediate or a register offset, as microcoded. Neither
compiler uses BTS on plain memory at -O2:
a[n / 64] |= 1UL << (n % 64)becomes a shift and an OR to memory. - 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 |
|---|---|---|
BTS m16/32/64, imm8
BTS_MEMv_IMMb | 0F BA /5 ib | Base instruction set |
BTS r16/32/64, imm8
BTS_GPRv_IMMb | 0F BA /5 ib | Base instruction set |
BTS m16/32/64, r16/32/64
BTS_MEMv_GPRv | 0F AB /r | Base instruction set |
BTS r16/32/64, r16/32/64
BTS_GPRv_GPRv | 0F AB /r | Base instruction set |
LOCK BTS m16/32/64, imm8
BTS_LOCK_MEMv_IMMb | 0F BA /5 ib | Base instruction set |
LOCK BTS m16/32/64, r16/32/64
BTS_LOCK_MEMv_GPRv | 0F AB /r | Base instruction set |
Latency and throughput
| Model | m16, imm8m32, imm8m64, imm8BTS m16, imm8BTS m32, imm8BTS m64, imm8 | r16, imm8r32, imm8r64, imm8r16, r16r32, r32 | m16, r16m32, r32BTS m16, r16BTS m32, r32 | m64, r64BTS 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.
BTS r/m16/32/64, imm8 BTS r16/32/64, r16/32/64 LOCK BTS 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.
BTS m16/32/64, r16/32/64 LOCK BTS 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 |
|---|---|---|
BTS r/m16/32/64, imm8 LOCK BTS m16/32/64, imm8 | ||
r/m16/32/64 | explicit | read, write |
imm8 | explicit | read |
RFLAGS | implicit | write |
BTS r/m16/32/64, r16/32/64 LOCK BTS 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-118 (BTS)
- 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.