Evo C++ Library v0.5.1
|
#include <evo/bits.h>
Traits and helpers for bit manipulation.
See BitArray for a bit array container.
T | Chunk value type for storing bits, must be an unsigned integer type or results are undefined |
TSize | Size integer type, must be unsigned |
Classes | |
struct | IterState |
Bit array iteration state. More... | |
Public Types | |
typedef TSize | Size |
Size integer type. More... | |
typedef T | Value |
Chunk value type. More... | |
Static Public Member Functions | |
static T | align_left (T value, uint count) |
Align bits on right to the left. More... | |
static T | align_right (T value, uint count) |
Align bits on left to the right. More... | |
static Size | array_bitsize (Size chunks) |
Calculate array size in bits for number of chunks. More... | |
static bool | array_checkall (const T *data, Size bitsize) |
Check if all bits are set in bit array. More... | |
static bool | array_checkall (const T *data, Size bitsize, Size pos, Size count) |
Check if all bits are set in subset of bit array. More... | |
static bool | array_checkany (const T *data, Size bitsize) |
Check if any bits are set in bit array. More... | |
static bool | array_checkany (const T *data, Size bitsize, Size pos, Size count) |
Check if any bits are set in subset of bit array. More... | |
static Size | array_copy (T *data, Size bitsize, T *src_data, Size src_bitsize, Size src_pos=0, Size src_count=ALL) |
Copy bits from another array. More... | |
static Size | array_countbits (const T *data, Size bitsize) |
Count number of set bits in bit array. More... | |
static Size | array_countbits (const T *data, Size bitsize, Size pos, Size count) |
Count number of set bits in subset of bit array. More... | |
template<class U = uint32> | |
static U | array_extractl (const T *data, Size bitsize, Size pos, Size count) |
Extract bits from chunked bit array. More... | |
template<class U = uint32> | |
static U | array_extractr (const T *data, Size bitsize, Size pos, Size count) |
Extract bits from chunked bit array. More... | |
static bool | array_get (const T *data, Size bitsize, Size pos) |
Get bit at position from chunked bit array. More... | |
static Size | array_iter (IterState &state, const T *data, Size bitsize) |
Iterate to first set bit in array. More... | |
static Size | array_iternext (IterState &state) |
Iterate to next set bit in array. More... | |
static bool | array_set (T *data, Size bitsize, Size pos, bool value=true) |
Set or clear bit at position in chunked bit array. More... | |
static Size | array_set_multi (T *data, Size bitsize, Size pos=0, Size count=ALL, bool value=true) |
Set or clear count bits at position in chunked bit array. More... | |
static void | array_shiftl (T *data, Size bitsize, uint count) |
Shift to the left in chunked bit array. More... | |
static void | array_shiftr (T *data, Size bitsize, uint count) |
Shift to the right in chunked bit array. More... | |
static Size | array_size (Size bitsize) |
Calculate array size in chunks for number of bits. More... | |
template<class U > | |
static bool | array_store (T *data, Size bitsize, Size pos, Size count, U value) |
Stores bits in chunked bit array. More... | |
static bool | array_toggle (T *data, Size bitsize, Size pos) |
Toggle bit at position in chunked bit array. More... | |
static Size | array_toggle_multi (T *data, Size bitsize, Size pos=0, Size count=ALL) |
Toggle count bits at position in chunked bit array. More... | |
static T | mask (uint start, uint count) |
Get bitmask with count bits set from start position. More... | |
static T | safemask (uint start, uint count) |
Get bitmask with count bits set from start position, with bounds checking for safety. More... | |
Static Public Attributes | |
static const T | ALLBITS = T(~ZERO) |
Value with all bits set. More... | |
static const uint | BITS = BYTES * 8 |
Number of bits for type T More... | |
static const uint | BITS_MINUS_1 = BITS - 1 |
BITS minus 1, used by bit manipulation code. More... | |
static const uint | BYTES = sizeof(T) |
Number of bytes for type T More... | |
static const T | LBIT = RBIT << BITS_MINUS_1 |
Mask with left-most bit set. More... | |
static const T | RBIT = 0x01 |
Mask with right-most bit set. More... | |
static const T | ZERO = 0 |
Zero value. More... | |
typedef TSize Size |
Size integer type.
typedef T Value |
Chunk value type.
|
inlinestatic |
Align bits on right to the left.
count
bits on the right are aligned on the leftvalue | Value to adjust |
count | Number of bits on the right side to align to the left side |
count
is too high
|
inlinestatic |
Align bits on left to the right.
count
bits on the left are aligned on the rightvalue | Value to adjust |
count | Number of bits on the left side to align to the right side |
count
is too high Calculate array size in bits for number of chunks.
chunks | Number of chunks in bit array |
|
inlinestatic |
Check if all bits are set in bit array.
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
Check if all bits are set in subset of bit array.
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
pos | Bit position to start at, 0 for first bit on left, etc |
count | Number of bits to check from pos, ALL for all from pos |
|
inlinestatic |
Check if any bits are set in bit array.
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
Check if any bits are set in subset of bit array.
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
pos | Bit position to start at, 0 for first bit on left, etc |
count | Number of bits to check from pos, ALL for all from pos |
|
inlinestatic |
Copy bits from another array.
data | Bit array data pointer to copy to, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array to copy to |
src_data | Source bit array data pointer, treated as a bool array even though bits come in chunks of T |
src_bitsize | Number of bits in source bit array |
src_pos | Source bit position to copy from, 0 for first bit on left, etc |
src_count | Number of bits to copy |
Count number of set bits in bit array.
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
Count number of set bits in subset of bit array.
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
pos | Bit position to start at, 0 for first bit on left, etc |
count | Number of bits to count from pos, ALL for all from pos |
Extract bits from chunked bit array.
111
returned as uint8
: 11100000
U | Return type for extracted bits, must be unsigned (uint, ulong, uint32, etc) – defaults to ulong in C++11 or newer |
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
pos | Bit position to extract from, 0 for first bit on left, etc |
count | Number of bits to extract, truncated if larger than bitsize or return value |
Extract bits from chunked bit array.
111
returned as uint8
: 00000111
or 7
in decimalcount
larger than bits in return type will be truncated – this should be avoided as it may not be easy to tell how many bits were truncatedcount=9
with return type uint8
then this uses count=8
count
may go out of bit array bounds without truncation – bits from out of bounds are set to 0 and still are right-aligned according to count
U | Return type for extracted bits, must be unsigned (uint, ulong, uint32, etc) – defaults to ulong in C++11 or newer |
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
pos | Bit position to extract from, 0 for first bit on left, etc |
count | Number of bits to extract, ALL for num bits in return type, truncated if larger than bits in return type |
Get bit at position from chunked bit array.
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
pos | Bit position to get, 0 for first bit on left, etc |
pos
is out of bounds Iterate to first set bit in array.
state | Stores iteration state for calling array_iternext() for next set bit [out] |
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
Iterate to next set bit in array.
state | Stores iteration state while iterating [in/out] |
Set or clear bit at position in chunked bit array.
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
pos | Bit position to set, 0 for first bit on left, etc |
value | Bit value to set, true to set bit, false to clear bit |
pos
out of bounds
|
inlinestatic |
Set or clear count bits at position in chunked bit array.
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
pos | Bit position to set, 0 for first bit on left, etc |
count | Number of bits to set from pos, ALL for all from pos |
value | Bit value to set, true to set bits, false to clear bits |
|
inlinestatic |
Shift to the left in chunked bit array.
data | Bit array data pointer |
bitsize | Number of bits in bit array |
count | Count to shift, must not be negative |
|
inlinestatic |
Shift to the right in chunked bit array.
data | Bit array data pointer |
bitsize | Number of bits in bit array |
count | Count to shift |
Calculate array size in chunks for number of bits.
bitsize | Number of bits in bit array |
Stores bits in chunked bit array.
00000111
with count=3
would store 111
(3 right-most bits) or 7
in decimalvalue
size, count is capped at that size – only bits from value
are storedU | Value type to store, signed value is treated as unsigned – inferred from value param |
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
pos | Bit position to store at, 0 for first bit on left, etc |
count | Number of bits to store from value |
value | Value to store (count right-most bits are stored) |
pos
is out of bounds Toggle bit at position in chunked bit array.
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
pos | Bit position to toggle, 0 for first bit on left, etc |
pos
out of bounds
|
inlinestatic |
Toggle count bits at position in chunked bit array.
data | Bit array data pointer, treated as a bool array even though bits come in chunks of T |
bitsize | Number of bits in bit array |
pos | Bit position to toggle, 0 for first bit on left, etc |
count | Number of bits to toggle from pos, ALL for all from pos |
|
inlinestatic |
Get bitmask with count
bits set from start
position.
start | Mask start position, 0 for first bit on left, etc |
count | Number of bits to have set from start |
|
inlinestatic |
Get bitmask with count
bits set from start
position, with bounds checking for safety.
start | Mask start position, 0 for first bit on left, etc |
count | Number of bits to have set from start, adjusted if out of bounds |
start
out of bounds or count
is 0
|
static |
Value with all bits set.
|
static |
Number of bits for type T
|
static |
BITS minus 1, used by bit manipulation code.
|
static |
Number of bytes for type T
|
static |
Mask with left-most bit set.
|
static |
Mask with right-most bit set.
|
static |
Zero value.