Expand description
The bit-int
crate is a library for providing arbitrary fixed bit-width
integers.
The value is represented by either of the following types:
- The
BitInt
type represents aN
-bit signed integer. This type is similar tosigned _BitInt(n)
in C23, or arbitrary fixed bit-width signed integer types (e.g.,i7
) in Zig. - The
BitUint
type represents aN
-bit unsigned integer. This type is similar tounsigned _BitInt(n)
in C23, or arbitrary fixed bit-width unsigned integer types (e.g.,u7
) in Zig.
The largest size of N
depends on the size of the underlying type in bits.
Therefore, when the underlying type of BitInt
is i32
, the largest
size of N
is i32::BITS
, and when the underlying type of BitUint
is
u64
, the largest size of N
is u64::BITS
.
§Examples
§Signed integer type
use bit_int::BitInt;
type Int = BitInt<i8, 7>;
let n = Int::MIN;
assert_eq!(format!("{n}"), "-64");
let n = n.checked_add(106).unwrap();
// Gets the contained value as the underlying type.
assert_eq!(n.get(), 42);
let n = n.checked_add(21).unwrap();
assert_eq!(n.get(), 63);
assert_eq!(n, Int::MAX);
assert_eq!(n.checked_add(22), None);
§Unsigned integer type
use bit_int::BitUint;
type Uint = BitUint<u8, 7>;
let n = Uint::MIN;
assert_eq!(format!("{n}"), "0");
let n = n.checked_add(42).unwrap();
// Gets the contained value as the underlying type.
assert_eq!(n.get(), 42);
let n = n.checked_add(85).unwrap();
assert_eq!(n.get(), 127);
assert_eq!(n, Uint::MAX);
assert_eq!(n.checked_add(86), None);
Structs§
- BitInt
BitInt
is a type that represents aN
-bit signed integer.- BitUint
BitUint
is a type that represents aN
-bit unsigned integer.
Type Aliases§
- BitI8
- A specialized
BitInt
type whose the underlying type is restricted toi8
. - BitI16
- A specialized
BitInt
type whose the underlying type is restricted toi16
. - BitI32
- A specialized
BitInt
type whose the underlying type is restricted toi32
. - BitI64
- A specialized
BitInt
type whose the underlying type is restricted toi64
. - BitI128
- A specialized
BitInt
type whose the underlying type is restricted toi128
. - BitIsize
- A specialized
BitInt
type whose the underlying type is restricted toisize
. - BitU8
- A specialized
BitUint
type whose the underlying type is restricted tou8
. - BitU16
- A specialized
BitUint
type whose the underlying type is restricted tou16
. - BitU32
- A specialized
BitUint
type whose the underlying type is restricted tou32
. - BitU64
- A specialized
BitUint
type whose the underlying type is restricted tou64
. - BitU128
- A specialized
BitUint
type whose the underlying type is restricted tou128
. - BitUsize
- A specialized
BitUint
type whose the underlying type is restricted tousize
.