# `btc`

[x86-64 Documentation](https://asm-editor.specy.app/documentation/x86.md) › [Instructions](https://asm-editor.specy.app/documentation/x86/instruction.md)

Web page: https://asm-editor.specy.app/documentation/x86/instruction/btc

**Bit test and complement**

Category: Logic

Copies the selected bit into CF, then complements that bit in the operand.

386 and later · accepts the `lock` prefix with a memory operand

[Intel's reference: BTC](https://intel.github.io/SDM/instruction/BTC.html)

## Flags

| Flag | Effect |
| --- | --- |
| CF | depends on the result |
| PF | undefined (do not rely on it) |
| AF | undefined (do not rely on it) |
| ZF | not changed |
| SF | undefined (do not rely on it) |
| DF | not changed |
| OF | undefined (do not rely on it) |

## Forms

- `btc r/m, reg` (16, 32, 64)
  - `btc r/m16, reg16`
  - `btc r/m32, reg32`
  - `btc r/m64, reg64`
- `btc r/m, imm8` (16, 32, 64)
  - `btc r/m16, imm8`
  - `btc r/m32, imm8`
  - `btc r/m64, imm8`

## Operands

- `r/m`: A general-purpose register or memory location matching the operation’s size, for example `rax` or `qword [rbp-8]`.
- `reg`: A general-purpose register matching the operation’s size, for example `al`, `ax`, `eax` or `rax`.
- `imm8`: An 8-bit immediate, `-128` to `255`. Instructions that sign-extend it to a wider operand interpret the byte as `-128` to `127`; shift counts do not use this sign extension.

## Description

`bt` leaves the operand unchanged; `btc` complements that bit, `btr` clears it and `bts` sets it.

For a register operand, the index wraps modulo its width: 16, 32 or 64 bits, so `bt rax, 63` tests the top bit of `rax`. For a memory operand, a register index is a signed bit offset into a bit string and can select a bit outside the first word, doubleword or quadword. An immediate index is one byte, and the processor takes it modulo the operand width even for a memory operand. NASM encodes the number as written, so `bt dword [rax], 40` tests bit 8 of the doubleword at `rax`. To reach a bit beyond the operand, move the address instead (`bt dword [rax + 4], 8` tests bit 40 of the string) or put the index in a register.

## Example

```x86
global _start

section .text
_start:
        mov rax, 12
        mov rbx, 5
        mov rcx, 3
        mov rsi, 9

        btc rax, rbx

        mov rax, 60         ; syscall 60: exit
        xor rdi, rdi
        syscall
```
