Skip to content

Instantly share code, notes, and snippets.

@andreasvc
Last active Dec 16, 2015
Embed
What would you like to do?
cdef extern from "macros.h":
# test whether the b'th bit of array a is set:
unsigned long TESTBIT(unsigned long a[], int b)
cdef unsigned long foo[2]
foo[0] = 281474976710656UL
foo[1] = 0
# what the macro does:
print foo[0] & (1UL << 48)
if (foo[0] & (1UL << 48)):
print 'bit 48 is set'
# macro works fine on its own:
print TESTBIT(foo, 48)
if TESTBIT(foo, 48) != 0:
print 'bit 48 is really set'
if TESTBIT(foo, 48):
print 'bit 48 is set'
print TESTBIT(foo, 48)
if True and TESTBIT(foo, 48) != 0:
print 'bit 48 is really set'
# bug occurs when combined with other condition:
if True and TESTBIT(foo, 48):
print 'bit 48 is set'
else:
print 'bit 48 is NOT set?'
#define BITSIZE (8*sizeof(long))
#define BITMASK(b) (1UL << ((b) % BITSIZE))
#define BITSLOT(b) ((b) / BITSIZE)
#define TESTBIT(a, b) ((a)[BITSLOT(b)] & BITMASK(b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment