bitmath
Tools for arbitrary-width bitwise arithmetic. Originally designed for writing fantasy console/cpu emulators.
Unstable. WIP. Use at your own risk.
Bits<N>
The heart of bitmath
. Bits
is a generically sized bit vector with support for bitwise operations and arithmetic (WIP).
For example, you can add a pair of 3-bit numbers and find out whether it overflowed:
let a = try_from.unwrap;
let b = try_from.unwrap;
let = a.unsigned_add;
println!;
// result: Bits<3>{ 011 | dec 3/3 | hex 0x3/0x3 }, overflowed: true
Or you can take a subset of Bits
with conventional bitwise syntax using the bitslice!
macro:
let word = try_from.unwrap;
let high_byte = bitslice!;
let low_byte = bitslice!;
println!;
// Bits<16>{ 1011 0001 0110 1011 | dec 45419/-20117 | hex 0xb16b/-0x4e95 }
// Bits<8>{ 1011 0001 | dec 177/-79 | hex 0xb1/-0x4f }
// Bits<8>{ 0110 1011 | dec 107/107 | hex 0x6b/0x6b }