Utilities for working with binary data and bit manipulation in Dart.
NOTE: Unless otherwise noted, all functionality is based around treating bits as little endian, that is in a 32-bit integer the leftmost bit is 31 and the rightmost bit is 0
This library supports an Integral
data type for fluent bit manipulation:
print(uint8.toBinaryPadded(196)); // '11000100'
Because of Dart's ability to do advanced inlining in both the Dart VM and
dart2js, this library should perform well and be extremely easy to use for most
use cases. For example, it's used in an arm7_tdmi
emulator.
This library has a combination of top-level methods, and instance methods of
pre-defined Integral
data types (see below). For example there are two ways
to clear (set to 0
) a bit:
// Sets the 0th bit in an (int) bits to 0
bits = clearBit(bits, 0)
However, this will not do range validation. Use Integral#clearBit
:
// Sets the 0th bit in a uint32 to 0.
// In dev-mode, if either bits or `n` is out of range it throws.
bits = uint32.clearBit(bits, 0);
bit
int4
int8
int16
int32
int64
int128
uint4
uint8
uint16
uint32
uint64
uint128
See the dartdocs for more about the API.