Skip to content

Instantly share code, notes, and snippets.

@andreasvc
Last active December 16, 2015 11:18
Show Gist options
  • Save andreasvc/5426158 to your computer and use it in GitHub Desktop.
Save andreasvc/5426158 to your computer and use it in GitHub Desktop.
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