Bit Test and Reset

Encoding

EncodingOperand 1Operand 2
mrModRM.reg[rw]ModRM.r/m[r]
miModRM.r/m[rw]imm8

Description

The BTS instruction selects a bit from the first source operand and stores it in CF. The specific bit that is selected is specified in the second source operand. Afterwards, the selected bit is reset (cleared) in the source operand.

The operand size determines how many bits of the second source operand are used; Any upper bits are ignored. For example, if the operand size is 16 bits, only the lowest five bits are used in the calculation (an effective "mod 16"). In other words, attempting to access the 21st bit of a 16-bit register will only access the fifth bit (21 mod 16 ≡ 5).

Operation

public void BTS(U16 src, U16 idx)
{
    EFLAGS.CF = src.Bit[idx % 16];
    src.Bit[idx % 16] = 1;
}

public void BTS(U32 src, U32 idx)
{
    EFLAGS.CF = src.Bit[idx % 32];
    src.Bit[idx % 32] = 1;
}

public void BTS(U64 src, U64 idx)
{
    EFLAGS.CF = src.Bit[idx % 64];
    src.Bit[idx % 64] = 1;
}

Flags Affected

CF (carry flag)
Set if the specified bit is set prior to being set. Cleared otherwise.
PF (parity flag)
Undefined.
AF (auxiliary flag)
Undefined.
ZF (zero flag)
Unmodified.
SF (sign flag)
Undefined.
OF (overflow flag)
Undefined.

Intrinsics

None. Auto-generated by compiler.

Exceptions

Real-Address Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand (using a segment other than
  • SS
  • ) has an effective address that is outside the segment's limit.

Virtual-8086 Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand (using a segment other than
  • SS
  • ) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.

Protected Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand uses a segment containing a NULL selector.
  • If a memory operand (using a segment other than SS) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.

Compatibility Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand uses a segment containing a NULL selector.
  • If a memory operand (using a segment other than SS) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.

Long Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the SS segment is in non-canonical form.
  • If a memory operand using the SS segment has an effective address that is outside the SS segment's limit.
#GP(0)
  • If a memory operand (using a segment other than SS) is in non-canonical form.
  • If a memory operand uses a segment containing a NULL selector.
  • If a memory operand (using a segment other than SS) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.