# `bit`

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

Web page: https://asm-editor.specy.app/documentation/z80/instruction/bit

**Test bit**

Category: Logic

Tests one bit of a register or memory byte and sets Z when that bit is zero.

Used in: [8-bit and 16-bit arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/z80/think-in-assembly/arithmetic-logic-and-bits.md) (Z80 assembly), [The screen, keyboard and mouse through ports](https://asm-editor.specy.app/learn/courses/z80/talking-to-the-outside-world/screen-keyboard-and-mouse.md) (Z80 assembly), [Even or odd, count the set bits, multiply by shifting](https://asm-editor.specy.app/learn/courses/z80/examples/bit-tricks.md) (Z80 assembly), [Flappy bird](https://asm-editor.specy.app/learn/courses/z80/examples/flappy-bird.md) (Z80 assembly)

## Flags

| Flag | Effect |
| --- | --- |
| S | undefined afterwards |
| Z | set from the result |
| H | always 1 |
| P/V | undefined afterwards |
| N | always 0 |
| C | not changed |

## Forms

- `bit b, r` · 2 bytes · 8 cycles · Tests bit `b` of `r`.
  - `bit 0, b`
  - `bit 0, c`
  - `bit 0, d`
  - `bit 0, e`
  - `bit 0, h`
  - `bit 0, l`
  - `bit 0, a`
  - `bit 1, b`
  - `bit 1, c`
  - `bit 1, d`
  - `bit 1, e`
  - `bit 1, h`
  - `bit 1, l`
  - `bit 1, a`
  - `bit 2, b`
  - `bit 2, c`
  - `bit 2, d`
  - `bit 2, e`
  - `bit 2, h`
  - `bit 2, l`
  - `bit 2, a`
  - `bit 3, b`
  - `bit 3, c`
  - `bit 3, d`
  - `bit 3, e`
  - `bit 3, h`
  - `bit 3, l`
  - `bit 3, a`
  - `bit 4, b`
  - `bit 4, c`
  - `bit 4, d`
  - `bit 4, e`
  - `bit 4, h`
  - `bit 4, l`
  - `bit 4, a`
  - `bit 5, b`
  - `bit 5, c`
  - `bit 5, d`
  - `bit 5, e`
  - `bit 5, h`
  - `bit 5, l`
  - `bit 5, a`
  - `bit 6, b`
  - `bit 6, c`
  - `bit 6, d`
  - `bit 6, e`
  - `bit 6, h`
  - `bit 6, l`
  - `bit 6, a`
  - `bit 7, b`
  - `bit 7, c`
  - `bit 7, d`
  - `bit 7, e`
  - `bit 7, h`
  - `bit 7, l`
  - `bit 7, a`

- `bit b, (hl)` · 2 bytes · 12 cycles · Tests bit `b` of (HL).
  - `bit 0, (hl)`
  - `bit 1, (hl)`
  - `bit 2, (hl)`
  - `bit 3, (hl)`
  - `bit 4, (hl)`
  - `bit 5, (hl)`
  - `bit 6, (hl)`
  - `bit 7, (hl)`

- `bit b, (ix+dd)` · 4 bytes · 20 cycles · Tests bit `b` of the memory location pointed to by IX plus `dd`.
  - `bit 0, (ix+dd)`
  - `bit 1, (ix+dd)`
  - `bit 2, (ix+dd)`
  - `bit 3, (ix+dd)`
  - `bit 4, (ix+dd)`
  - `bit 5, (ix+dd)`
  - `bit 6, (ix+dd)`
  - `bit 7, (ix+dd)`
  - `bit 0, (iy+dd)`
  - `bit 1, (iy+dd)`
  - `bit 2, (iy+dd)`
  - `bit 3, (iy+dd)`
  - `bit 4, (iy+dd)`
  - `bit 5, (iy+dd)`
  - `bit 6, (iy+dd)`
  - `bit 7, (iy+dd)`

## Operands

- `b`: A bit number from `0` to `7`, where bit `0` is the least significant bit.
- `r`: An 8-bit register: `a`, `b`, `c`, `d`, `e`, `h` or `l`.
- [`(hl)`](https://asm-editor.specy.app/documentation/z80/registers/operand-hl.md): The byte in memory at the address stored in `hl`.
- [`(ix+dd)`](https://asm-editor.specy.app/documentation/z80/registers/operand-ix-dd.md): The byte in memory at `ix + dd`, where `dd` is a signed displacement from `-128` to `127`, for example `(ix+5)` or `(ix-3)`. `(iy+dd)` works the same way.

## Example

```z80
        .org 0x8000
        ld b, 3
        bit 0, b
        halt
```
