reworked pattern generator

This commit is contained in:
2026-07-09 18:59:41 +02:00
parent b4a3e1187a
commit 46623c946a
9 changed files with 277 additions and 299 deletions
+17 -22
View File
@@ -1,31 +1,26 @@
import numpy as np
def _bitmask(bit, word):
dtype = word.dtype if hasattr(word, 'dtype') else np.uint64
if bit >= np.iinfo(dtype).bits:
raise ValueError(f"bit {bit} out of range for {np.dtype(dtype).name}")
return np.dtype(dtype).type(1 << bit)
def setbit(bit, word):
if isinstance(word, np.generic):
mask = word.dtype.type(1)
mask = mask << bit
else:
mask = 1 << bit
return word | mask
def setbit_arr(bit, arr):
arr |= arr.dtype.type(1 << bit)
"""
Set the bit at position bit in word(s).
"""
return word | _bitmask(bit, word)
def clearbit(bit, word):
"""
Clear the bit at position bit in word.
Two paths to avoid converting the types.
Clear the bit at position bit in word(s).
"""
if isinstance(word, np.generic):
mask = word.dtype.type(1)
mask = ~(mask << bit)
else:
mask = ~(1 << bit)
return word & mask
return word & ~_bitmask(bit, word)
def clearbit_arr(bit, arr):
arr &= arr.dtype.type(~(1 << bit))
def flipbit(bit, word):
"""
Flip the bit at position bit in word(s).
"""
return word ^ _bitmask(bit, word)