A arbitrary-precision big integer, with a fixed set of immutable limbs.
Fields
limbs: []const Limb,
Raw digits. These are:
- Little-endian ordered
- limbs.len >= 1
- Zero is represented as limbs.len == 1 with limbs[0] == 0.
Accessing limbs directly should be avoided.
positive: bool,
Functions
fn bitCountAbs(self: Const) usize
Returns the number of bits required to represent the absolute value of an intege…
Returns the number of bits required to represent the absolute value of an integer.
fn bitCountTwosComp(self: Const) usize
Returns the number of bits required to represent the integer in twos-complement …
Returns the number of bits required to represent the integer in twos-complement form.
If the integer is negative the value returned is the number of bits needed by a signed integer to represent the value. If positive the value is the number of bits for an unsigned integer. Any unsigned integer will fit in the signed integer with bitcount one greater than the returned value.
e.g. -127 returns 8 as it will fit in an i8. 127 returns 7 since it fits in a u7.
fn fits(self: Const, comptime T: type) bool
Returns whether self can fit into an integer of the requested type.
fn fitsInTwosComp(self: Const, signedness: Signedness, bit_count: usize) bool
No documentation provided.
fn format(self: Const, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: anytype) !void
To allow
std.fmt.format
to work with this type. If the integer is larger than…To allow
std.fmt.format
to work with this type. If the integer is larger thanpow(2, 64 * @sizeOf(usize) * 8), this function will fail to print the string, printing "(BigInt)" instead of a number. This is because the rendering algorithm requires reversing a string, which requires O(N) memory. See
toStringand
toStringAlloc` for a way to print big integers without failure.fn order(a: Const, b: Const) math.Order
Returns
math.Order.lt
,math.Order.eq
,math.Order.gt
ifa < b
,a == b
o…Returns
math.Order.lt
,math.Order.eq
,math.Order.gt
ifa < b
,a == b
ora > b
respectively.fn orderAbs(a: Const, b: Const) math.Order
Returns
math.Order.lt
,math.Order.eq
,math.Order.gt
if|a| < |b|
, `|a| …Returns
math.Order.lt
,math.Order.eq
,math.Order.gt
if|a| < |b|
,|a| == |b|
, or|a| > |b|
respectively.fn orderAgainstScalar(lhs: Const, scalar: anytype) math.Order
Same as
order
but the right-hand operand is a primitive integer.fn popCount(self: Const, bit_count: usize) usize
@popCount with two’s complement semantics.
@popCount with two’s complement semantics.
This returns the number of 1 bits set when the value would be represented in two’s complement with the given integer width (bit_count). This includes the leading sign bit, which will be set for negative values.
Asserts that bit_count is enough to represent value in two’s compliment and that the final result fits in a usize. Asserts that there are no trailing empty limbs on the most significant end, i.e. that limb count matches
calcLimbLen()
and zero is not negative.fn sizeInBaseUpperBound(self: Const, base: usize) usize
Returns the approximate size of the integer in the given base. Negative values a…
Returns the approximate size of the integer in the given base. Negative values accommodate for the minus sign. This is used for determining the number of characters needed to print the value. It is inexact and may exceed the given value by ~1-2 bytes. TODO See if we can make this exact.
fn to(self: Const, comptime T: type) ConvertError!T
Convert self to type T.
Convert self to type T.
Returns an error if self cannot be narrowed into the requested type without truncation.
fn toManaged(self: Const, allocator: Allocator) Allocator.Error!Managed
The result is an independent resource which is managed by the caller.
fn toString(self: Const, string: []u8, base: u8, case: std.fmt.Case, limbs_buffer: []Limb) usize
Converts self to a string in the requested base. Asserts that
base
is in the …Converts self to a string in the requested base. Asserts that
base
is in the range [2, 16].string
is a caller-provided slice of at leastsizeInBaseUpperBound
bytes, where the result is written to. Returns the length of the string.limbs_buffer
is caller-provided memory fortoString
to use as a working area. It must have length of at leastcalcToStringLimbsBufferLen
. In the case of power-of-two base,limbs_buffer
is ignored. See alsotoStringAlloc
, a higher level function than this.fn toStringAlloc(self: Const, allocator: Allocator, base: u8, case: std.fmt.Case) Allocator.Error![]u8
Converts self to a string in the requested base. Caller owns returned memory. …
Converts self to a string in the requested base. Caller owns returned memory. Asserts that
base
is in the range [2, 16]. See alsotoString
, a lower level function than this.fn writePackedTwosComplement(x: Const, buffer: []u8, bit_offset: usize, bit_count: usize, endian: Endian) void
Write the value of
x
to a packed memorybuffer
. Asserts thatbuffer
is la…Write the value of
x
to a packed memorybuffer
. Asserts thatbuffer
is large enough to contain a value of bit-sizebit_count
at offsetbit_offset
.This is equivalent to storing the value of an integer with
bit_count
bits as if it were a field in packed memory at the provided bit offset.fn writeTwosComplement(x: Const, buffer: []u8, endian: Endian) void
Write the value of
x
intobuffer
Asserts thatbuffer
is large enough to s…Write the value of
x
intobuffer
Asserts thatbuffer
is large enough to store the value.buffer
is filled so that its contents match what would be observed via @ptrCast(*[buffer.len]const u8, &x). Byte ordering is determined byendian
, and any required padding bits are added on the MSB end.