# `bt`

[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/bt

**Bit test**

Category: Logic

Copies the bit at the index given by the second operand into CF, leaving the operand unchanged.

386 and later

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

Used in: [Arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/arithmetic-logic-and-bits.md) (x86-64 assembly), [Even or odd, count the set bits, multiply by shifting](https://asm-editor.specy.app/learn/courses/x86/examples/bit-tricks.md) (x86-64 assembly)

## 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

- `bt r/m, reg` (16, 32, 64)
  - `bt r/m16, reg16`
  - `bt r/m32, reg32`
  - `bt r/m64, reg64`
- `bt r/m, imm8` (16, 32, 64)
  - `bt r/m16, imm8`
  - `bt r/m32, imm8`
  - `bt 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

        bt rax, rbx

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