IA-32 Assembly Language Reference Manual

Bit Instructions

Bit Scan Forward (bsf)

bsf{wl}		r/m[16|32], reg[16|32]

Operation

(r/m = 0) 0 -> ZF (r/m [ne ] 0) 0 -> ZF

Description

bsf scans the bits, starting at bit 0, in the doubleword operand or the second word. If the bits are all zero, ZF is cleared. Otherwise, ZF is set and the bit index of the first set bit, found while scanning in the forward direction, is loaded into the destination register.

Example
bsf 4(%edi), %edx

Bit Scan Reverse (bsr)

bsr{wl}		r/m[16|32], reg[16|32]
Operation

(r/m = 0) 0 -> ZF (r/m [ne ] 0) 0 -> ZF

Description

bsr scans the bits, starting at the most significant bit, in the doubleword operand or the second word. If the bits are all zero, ZF is cleared. Otherwise, ZF is set and the bit index of the first set bit found, while scanning in the reverse direction, is loaded into the destination register

Example
bsr 4(%edi), %edx

Bit Test (bt)

bt{wl}		imm8, r/m[16|32]
bt{wl}		reg[16|32], r/m[16|32]
Operation

BIT [LeftSRC, RightSRC] -> CF

Description

The bit indicated by the first operand (base) and the second operand (offset) are saved by bt into CF (carry flag).

Example

btl $253, 4(%edi)
btl %edx, 4(%edi)

Bit Test And Complement (btc)

btc{wl}		imm8, r/m[16|32] btc{wl}		reg[16|32], r/m[16|32]
Operation

BIT [LeftSRC, RightSRC] -> CF

NOT BIT [LeftSRC, RightSRC] -> BIT[LeftSRC, RightSRC]

Description

The bit indicated by the first operand (base) and the second operand (offset) are saved by btc into CF (carry flag) and complements the bit.

Example
btl $253, 4(%edi)
btl %edx, 4(%edi)

Bit Test And Reset (btr)

btr{wl}		imm8, r/m[16|32]
btr{wl}		reg[16|32], r/m[16|32]
Operation

BIT[LeftSRC, RightSRC] -> CF

0 -> BIT[LeftSRC, RightSRC]

Description

The value of the first operand (base) and the second operand (bit offset) are saved by btr into the carry flag and then it stores 0 in the bit.

Example
btrl $253, 4(%edi)
 btrl $edx, 4(%edi)

Bit Test And Set (bts)

bts{wl}      imm8, r/m[16|32]
bts{wl}      reg[16|32], r/m[16|32]
Operation

BIT[LeftSRC, RightSRC] -> CF

0 -> BIT[LeftSRC, RightSRC]

Description

The value of the first operand (base) and the second operand (bit offset) are saved by bts into the carry flag and then it stores 1 in the bit.

Example
btsl $253, 4(%edi)
 btsl $edx, 4(%edi)