This topic has not yet been rated - Rate this topic

_interlockedbittestandset, _interlockedbittestandset64

Updated: March 2011

Microsoft Specific

Generate the lock bts instruction, which examines bit b of the address a and returns its current value before setting it to 1.

unsigned char _interlockedbittestandset(
   long *a,
   long b
);
unsigned char _interlockedbittestandset64(
   __int64 *a,
   __int64 b
);
[in] a

A pointer to the memory to examine.

[in] b

The bit position to test.

The bit at the position specified before it is set.

Intrinsic

Architecture

_interlockedbittestandset

x86, x64

_interlockedbittestandset64

x64

Header file <intrin.h>

As a result of the lock bts instruction, the bit is set to 1. The operation is atomic.

These functions behave as read-write memory barriers. For more information, see _ReadWriteBarrier.

This routine is only available as an intrinsic.

Date

History

Reason

March 2011

Clarified the return value.

Information enhancement.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Older SDKs have an error
Error    1    error C2733: second C linkage of overloaded function '_interlockedbittestandset' not allowed    $VCDIR\include\intrin.h    944
Error    2    error C2733: second C linkage of overloaded function '_interlockedbittestandreset' not allowed    $VCDIR\include\intrin.h    945
This is due to the fact that WinNT.h and intrin.h both declare those functions (set and reset and their 64 bit counterparts). One workaround is this:

#if _MSC_VER >= 1400
//  Following 8 lines: workaround for a bug in some older SDKs
#   pragma push_macro("_interlockedbittestandset")
#   pragma push_macro("_interlockedbittestandreset")
#   pragma push_macro("_interlockedbittestandset64")
#   pragma push_macro("_interlockedbittestandreset64")
#   define _interlockedbittestandset _local_interlockedbittestandset
#   define _interlockedbittestandreset _local_interlockedbittestandreset
#   define _interlockedbittestandset64 _local_interlockedbittestandset64
#   define _interlockedbittestandreset64 _local_interlockedbittestandreset64
#   include <intrin.h> // to force the header not to be included elsewhere
#   pragma pop_macro("_interlockedbittestandreset64")
#   pragma pop_macro("_interlockedbittestandset64")
#   pragma pop_macro("_interlockedbittestandreset")
#   pragma pop_macro("_interlockedbittestandset")
#endif

It should be okay to include this before or after WinNT.h.