共用方式為


_bittestandreset, _bittestandreset64

Microsoft Specific

Generate the btr instruction, which examines bit b of the address a, returns its current value, and resets the bit to 0.

unsigned char _bittestandreset(
   long *a,
   long b
);
unsigned char _bittestandreset64(
   __int64 *a,
   __int64 b
);

Parameters

  • [in, out] a
    A pointer to the memory to examine.

  • [in] b
    The bit position to test.

Return Value

The bit at the position specified.

Requirements

Intrinsic

Architecture

_bittestandreset

x86, IPF, x64

_bittestandreset64

IPF, x64

Header file <intrin.h>

Remarks

On the IPF architecture, the btr instruction is not available, so this intrinsic is a custom function that imitates the behavior of btr. This custom function might be slower than a hand-written inline function because it includes overhead, such as handling the case where b is negative, that might be unnecessary in specific cases.

This routine is only available as an intrinsic.

Example

// bittestandreset.cpp
// processor: x86, IPF, x64
#include <stdio.h>
#include <limits.h>
#include <intrin.h>

#pragma intrinsic(_bittestandreset)

// Check the sign bit and reset to 0 (taking the absolute value)
// Returns 0 if the number is positive or zero
// Returns 1 if the number is negative
unsigned char absolute_value(long* p)
{
   const int SIGN_BIT = 31;
   return _bittestandreset(p, SIGN_BIT);
}

int main()
{
    long i = -112;
    unsigned char result;
   
    // Check the sign bit and reset to 0 (taking the absolute value)

    result = absolute_value(&i);
    if (result == 1)
        printf_s("The number was negative.\n");   
}
The number was negative.

See Also

Reference

Compiler Intrinsics