Bit::Vector - Efficient bit vector, set of integers and "big int" math library
| Use Case | Command | Description |
|---|---|---|
| đĻ Create a new bit vector | Bit::Vector->new($bits) | Create a new bit vector with $bits bits, all cleared. |
| đĸ Create from hex string | Bit::Vector->new_Hex($bits, $hex) | Create and initialize from hexadecimal string. |
| đ Set a single bit | $vec->Bit_On($index) | Set bit at $index to 1. |
| đ Clear a single bit | $vec->Bit_Off($index) | Set bit at $index to 0. |
| đ Test a bit | $vec->bit_test($index) | Return 1 if bit is set, 0 otherwise. |
| đ Bitwise OR (Union) | $vec3->Or($vec1, $vec2) | Compute bitwise OR of two vectors. |
| đ Bitwise AND (Intersection) | $vec3->And($vec1, $vec2) | Compute bitwise AND of two vectors. |
| đ Bitwise NOT (Complement) | $vec2->Not($vec1) | Compute bitwise complement (flip all bits). |
| â Arithmetic addition | $vec3->add($vec1, $vec2, $carry) | Add two numbers with carry, store result. |
| â Arithmetic subtraction | $vec3->subtract($vec1, $vec2, $carry) | Subtract two numbers with borrow. |
| âī¸ Multiplication | $vec3->Multiply($vec1, $vec2) | Multiply two signed numbers. |
| đĸ Convert to hex string | $vec->to_Hex() | Return hexadecimal representation. |
| đĸ Convert from hex string | $vec->from_Hex($hex) | Load vector from hexadecimal string. |
| đ Get size | $vec->Size() | Return number of bits in vector. |
| đ Resize | $vec->Resize($bits) | Change size, preserving as many bits as possible. |
$version = Bit::Vector->Version(); â Returns version number.$bits = Bit::Vector->Word_Bits(); â Bits in a machine word (unsigned int).$bits = Bit::Vector->Long_Bits(); â Bits in an unsigned long.$vector = Bit::Vector->new($bits); â Bit vector constructor.@veclist = Bit::Vector->new($bits, $count); â Create multiple vectors.$vector = Bit::Vector->new_Hex($bits, $string); â Constructor with hex init.$vector = Bit::Vector->new_Bin($bits, $string); â Constructor with binary init.$vector = Bit::Vector->new_Dec($bits, $string); â Constructor with decimal init.$vector = Bit::Vector->new_Enum($bits, $string); â Constructor with enumeration init.$vector = Bit::Vector->Concat_List(@vectors); â Concatenate multiple vectors.$vec2 = $vec1->new($bits); â Alternative constructor call.@veclist = $vec->new($bits, $count); â Alternative multiple constructor.$vec2 = $vec1->Shadow(); â New vector, same size, empty.$vec2 = $vec1->Clone(); â Exact duplicate.$vector = $vec1->Concat($vec2); â Concatenate two vectors.$vector = $vec1->Concat_List($vec2, $vec3, ...); â Concatenate multiple.$bits = $vector->Size(); â Number of bits.$vector->Resize($bits); â Change size.$vec2->Copy($vec1); â Copy contents.$vector->Empty(); â Clear all bits.$vector->Fill(); â Set all bits.$vector->Flip(); â Complement all bits.$vector->Primes(); â Sieve of Eratosthenes: set bits at prime indices.$vec2->Reverse($vec1); â Reverse order of bits.$vector->Interval_Empty($min, $max); â Clear bit range.$vector->Interval_Fill($min, $max); â Set bit range.$vector->Interval_Flip($min, $max); â Complement bit range.$vector->Interval_Reverse($min, $max); â Reverse bit range.($min, $max) = $vector->Interval_Scan_inc($start); â Find next block of set bits (ascending).($min, $max) = $vector->Interval_Scan_dec($start); â Find next block of set bits (descending).$vec2->Interval_Copy($vec1, $offset2, $offset1, $length); â Copy bit range.$vec2->Interval_Substitute($vec1, $off2, $len2, $off1, $len1); â Splice-like operation.if ($vector->is_empty()) â Test if all bits cleared.if ($vector->is_full()) â Test if all bits set.if ($vec1->equal($vec2)) â Equality test.$cmp = $vec1->Lexicompare($vec2); â Unsigned comparison.$cmp = $vec1->Compare($vec2); â Signed comparison.$string = $vector->to_Hex(); â Convert to hex string.$vector->from_Hex($string); â Load from hex string.$string = $vector->to_Bin(); â Convert to binary string.$vector->from_Bin($string); â Load from binary string.$string = $vector->to_Dec(); â Convert to decimal string.$vector->from_Dec($string); â Load from decimal string.$string = $vector->to_Enum(); â Convert to enumeration string (e.g., "2,3,5-7").$vector->from_Enum($string); â Load from enumeration string.$vector->Bit_Off($index); â Clear single bit.$vector->Bit_On($index); â Set single bit.$bit = $vector->bit_flip($index); â Flip single bit, return new state.$bit = $vector->bit_test($index); â Test single bit.$vector->Bit_Copy($index, $bit); â Set single bit to 0 or 1.$vector->LSB($bit); â Set least significant bit.$vector->MSB($bit); â Set most significant bit.$bit = $vector->lsb(); â Get least significant bit.$bit = $vector->msb(); â Get most significant bit.$carry = $vector->rotate_left(); â Rotate left through carry.$carry = $vector->rotate_right(); â Rotate right through carry.$carry = $vector->shift_left($carry_in); â Shift left with carry.$carry = $vector->shift_right($carry_in); â Shift right with carry.$vector->Move_Left($bits); â Shift left by N positions.$vector->Move_Right($bits); â Shift right by N positions.$vector->Insert($offset, $bits); â Insert bits at offset (size unchanged).$vector->Delete($offset, $bits); â Delete bits at offset (size unchanged).$carry = $vector->increment(); â Increment (unsigned).$carry = $vector->decrement(); â Decrement (unsigned).$overflow = $vec2->inc($vec1); â Increment and copy.$overflow = $vec2->dec($vec1); â Decrement and copy.$carry = $vec3->add($vec1, $vec2, $carry); â Addition with carry.($carry, $overflow) = $vec3->add($vec1, $vec2, $carry); â Addition with carry and overflow.$carry = $vec3->subtract($vec1, $vec2, $carry); â Subtraction with borrow.($carry, $overflow) = $vec3->subtract($vec1, $vec2, $carry); â Subtraction with borrow and overflow.$vec2->Neg($vec1); â Two's complement (negate).$vec2->Negate($vec1); â Alias for Neg.$vec2->Abs($vec1); â Absolute value.$vec2->Absolute($vec1); â Alias for Abs.$sign = $vector->Sign(); â Return 0, 1, or -1.$vec3->Multiply($vec1, $vec2); â Multiplication (signed).$quot->Divide($vec1, $vec2, $rest); â Division and remainder.$vecgcd->GCD($veca, $vecb); â Greatest Common Divisor.$vecgcd->GCD($vecx, $vecy, $veca, $vecb); â Extended GCD (linear combination).$vec3->Power($vec1, $vec2); â Exponentiation.$vector->Block_Store($buffer); â Load from binary string (low byte first).$buffer = $vector->Block_Read(); â Export to binary string.$size = $vector->Word_Size(); â Number of machine words.$vector->Word_Store($offset, $word); â Store a machine word.$word = $vector->Word_Read($offset); â Read a machine word.$vector->Word_List_Store(@words); â Store list of words.@words = $vector->Word_List_Read(); â Read all words.$vector->Word_Insert($offset, $count); â Insert machine words.$vector->Word_Delete($offset, $count); â Delete machine words.$vector->Chunk_Store($chunksize, $offset, $chunk); â Store a chunk of bits.$chunk = $vector->Chunk_Read($chunksize, $offset); â Read a chunk of bits.$vector->Chunk_List_Store($chunksize, @chunks); â Store list of chunks.@chunks = $vector->Chunk_List_Read($chunksize); â Read list of chunks.$vector->Index_List_Remove(@indices); â Clear bits at indices.$vector->Index_List_Store(@indices); â Set bits at indices.@indices = $vector->Index_List_Read(); â Get list of set bit indices.$vec3->Or($vec1, $vec2); â Bitwise OR (Union).$set3->Union($set1, $set2); â Alias.$vec3->And($vec1, $vec2); â Bitwise AND (Intersection).$set3->Intersection($set1, $set2); â Alias.$vec3->AndNot($vec1, $vec2); â Bitwise AND NOT (Difference).$set3->Difference($set1, $set2); â Alias.$vec3->Xor($vec1, $vec2); â Bitwise XOR (Symmetric difference).$set3->ExclusiveOr($set1, $set2); â Alias.$vec2->Not($vec1); â Bitwise NOT (Complement).$set2->Complement($set1); â Alias.if ($set1->subset($set2)) â Subset test.$norm = $set->Norm(); â Count set bits (fast).$norm = $set->Norm2(); â Count set bits (alternative).$norm = $set->Norm3(); â Count set bits (sparse).$min = $set->Min(); â Minimum index of set bit.$max = $set->Max(); â Maximum index of set bit.$m3->Multiplication($r3, $c3, $m1, $r1, $c1, $m2, $r2, $c2); â Boolean matrix multiplication (XOR addition).$m3->Product($r3, $c3, $m1, $r1, $c1, $m2, $r2, $c2); â Boolean matrix multiplication (OR addition).$matrix->Closure($rows, $cols); â Reflexive transitive closure (Kleene).$matrix2->Transpose($rows2, $cols2, $matrix1, $rows1, $cols1); â Matrix transpose.new().)Block_Read()/Block_Store() use "least order byte first" for portability. In hex/binary strings, rightmost bit is least significant.Word_* are machine-dependent! Use Chunk_* with chunk size ⤠32 bits for portability.Bit::Vector->Long_Bits(). For portability, use ⤠32.Concat(), Concat_List(), Copy(), Interval_Copy(), Interval_Substitute(). Multiply() allows result vector to be larger. Power() result must be same size or larger than base.$vector->Size()-1.Bit::Vector objects can be serialized with Storable automatically.$version = Bit::Vector->Version(); â Returns the current version number of this module.$bits = Bit::Vector->Word_Bits(); â Returns the number of bits of an unsigned int (machine word) on your machine.$bits = Bit::Vector->Long_Bits(); â Returns the number of bits of an unsigned long on your machine.$vector = Bit::Vector->new($bits); â Bit vector constructor. Creates a new vector with $bits bits, all cleared. $bits = 0 is permitted. An exception is raised if memory allocation fails.@veclist = Bit::Vector->new($bits, $count); â Creates a list of $count bit vectors, all with $bits bits and cleared. Returns empty list if $count = 0.$vector = Bit::Vector->new_Hex($bits, $string); â Alternative constructor: creates vector and initializes from hex string. More efficient than separate new() + from_Hex(). Raises exception on memory or syntax error.$vector = Bit::Vector->new_Bin($bits, $string); â Constructor with binary string initialization.$vector = Bit::Vector->new_Dec($bits, $string); â Constructor with decimal string initialization.$vector = Bit::Vector->new_Enum($bits, $string); â Constructor with enumeration string initialization (e.g., "2,3,5-7").$vector = Bit::Vector->Concat_List(@vectors); â Creates a new vector by concatenating all given vectors. Rightmost vector becomes least significant part. Empty list returns zero-length vector.$vec2 = $vec1->new($bits); â Alternative way to call constructor, using an existing vector as anchor.@veclist = $vec->new($bits, $count); â Same for multiple vectors.$vec2 = $vec1->Shadow(); â Creates a new vector of the same size but empty.$vec2 = $vec1->Clone(); â Creates an exact duplicate.$vector = $vec1->Concat($vec2); â Concatenates $vec1 (most significant) and $vec2 (least significant).$vector = $vec1->Concat_List($vec2, $vec3, ...); â Concatenates all arguments.$bits = $vector->Size(); â Returns the size (number of bits).$vector->Resize($bits); â Changes size, preserving as many bits as will fit. May reuse memory if same number of words. Raises exception on memory failure.$vec2->Copy($vec1); â Copies $vec1 into $vec2 (both must exist). Handles sign extension if source larger or smaller.$vector->Empty(); â Clears all bits.$vector->Fill(); â Sets all bits.$vector->Flip(); â Complements all bits.$vector->Primes(); â Sets bits at prime indices using Sieve of Eratosthenes.$vec2->Reverse($vec1); â Reverses bit order (LSB â MSB). In-place supported.$vector->Interval_Empty($min, $max); â Clears bits in range [min, max].$vector->Interval_Fill($min, $max); â Sets bits in range.$vector->Interval_Flip($min, $max); â Complements bits in range.$vector->Interval_Reverse($min, $max); â Reverses order of bits in range.($min, $max) = $vector->Interval_Scan_inc($start); â Finds next contiguous block of set bits starting from $start (ascending). Returns empty list if none.($min, $max) = $vector->Interval_Scan_dec($start); â Same, descending.$vec2->Interval_Copy($vec1, $offset2, $offset1, $length); â Copies a stretch of bits from $vec1 to $vec2. Length is automatically adjusted if out of bounds.$vec2->Interval_Substitute($vec1, $off2, $len2, $off1, $len1); â Splice-like operation: replaces a stretch in $vec2 with a stretch from $vec1, automatically resizing $vec2. If $len1=0, deletes; if $len2=0, inserts.if ($vector->is_empty()) â Returns true if all bits are cleared (zero). Also true for zero-length vector.if ($vector->is_full()) â Returns true if all bits are set (-1). False for zero-length.if ($vec1->equal($vec2)) â Returns true if vectors are identical.$cmp = $vec1->Lexicompare($vec2); â Compares unsigned numbers: -1, 0, 1.$cmp = $vec1->Compare($vec2); â Compares signed numbers: -1, 0, 1.$string = $vector->to_Hex(); â Returns hex string (LSB at right).$vector->from_Hex($string); â Loads from hex string; extra digits ignored, missing digits zero-filled.$string = $vector->to_Bin(); â Returns binary string (LSB at right).$vector->from_Bin($string); â Loads from binary string.$string = $vector->to_Dec(); â Returns decimal string (signed). May be slow for large numbers.$vector->from_Dec($string); â Loads from decimal string (signed). Raises overflow if number too large.$string = $vector->to_Enum(); â Returns enumeration string like "2,3,5-7".$vector->from_Enum($string); â Loads from enumeration string (indices and ranges).$vector->Bit_Off($index); â Clears single bit.$vector->Bit_On($index); â Sets single bit.$bit = $vector->bit_flip($index); â Flips single bit, returns new state.$bit = $vector->bit_test($index); â Returns state of bit (0 or 1).$vector->Bit_Copy($index, $bit); â Sets bit to 0 or 1.$vector->LSB($bit); â Sets least significant bit.$vector->MSB($bit); â Sets most significant bit.$bit = $vector->lsb(); â Returns least significant bit.$bit = $vector->msb(); â Returns most significant bit.$carry = $vector->rotate_left(); â Rotates left through carry (MSB moves to LSB via carry).$carry = $vector->rotate_right(); â Rotates right through carry.$carry = $vector->shift_left($carry_in); â Shifts left, LSB filled with $carry_in, MSB becomes new carry.$carry = $vector->shift_right($carry_in); â Shifts right, MSB filled with $carry_in, LSB becomes new carry.$vector->Move_Left($bits); â Shifts left by $bits positions, inserts zeros at LSB, discards MSB bits.$vector->Move_Right($bits); â Shifts right by $bits positions, inserts zeros at MSB, discards LSB bits.$vector->Insert($offset, $bits); â Inserts $bits empty bits at offset, shifting higher bits up, losing top bits. Does not change size.$vector->Delete($offset, $bits); â Removes $bits at offset, shifting higher bits down, clearing top bits. Does not change size.$carry = $vector->increment(); â Increments (unsigned). Returns carry if overflow.$carry = $vector->decrement(); â Decrements (unsigned). Returns carry if underflow.$overflow = $vec2->inc($vec1); â Copies $vec1 to $vec2, increments copy, returns overflow flag.$overflow = $vec2->dec($vec1); â Copies $vec1 to $vec2, decrements copy, returns overflow flag.$carry = $vec3->add($vec1, $vec2, $carry); â Addition with carry. In list context returns (carry, overflow).$carry = $vec3->subtract($vec1, $vec2, $carry); â Subtraction with borrow. In list context returns (carry, overflow).$vec2->Neg($vec1); â Two's complement (negate).$vec2->Negate($vec1); â Alias.$vec2->Abs($vec1); â Absolute value.$vec2->Absolute($vec1); â Alias.$sign = $vector->Sign(); â Returns 0 if zero, -1 if negative, 1 if positive.$vec3->Multiply($vec1, $vec2); â Multiplication (signed). Result vector may be larger than factors.$quot->Divide($vec1, $vec2, $rest); â Division and remainder (signed). $quot and $rest must be distinct.$vecgcd->GCD($veca, $vecb); â Greatest Common Divisor (Euclid's algorithm).$vecgcd->GCD($vecx, $vecy, $veca, $vecb); â Extended GCD, returns coefficients x, y such that GCD = x*a + y*b.$vec3->Power($vec1, $vec2); â Exponentiation (base^exponent). Result must be same size or larger than base. Exponent must be positive.$vector->Block_Store($buffer); â Loads bit vector from binary string (little-endian byte order).$buffer = $vector->Block_Read(); â Exports bit vector to binary string (little-endian).$size = $vector->Word_Size(); â Number of machine words (machine-dependent).$vector->Word_Store($offset, $word); â Stores a machine word at offset.$word = $vector->Word_Read($offset); â Reads a machine word at offset.$vector->Word_List_Store(@words); â Stores list of words (LSB first).@words = $vector->Word_List_Read(); â Reads all words (LSB first).$vector->Word_Insert($offset, $count); â Inserts $count empty words, losing top words.$vector->Word_Delete($offset, $count); â Deletes $count words, clearing top words.$vector->Chunk_Store($chunksize, $offset, $chunk); â Stores a chunk of bits (up to Long_Bits).$chunk = $vector->Chunk_Read($chunksize, $offset); â Reads a chunk of bits.$vector->Chunk_List_Store($chunksize, @chunks); â Stores list of chunks (LSB first).@chunks = $vector->Chunk_List_Read($chunksize); â Reads list of chunks.$vector->Index_List_Remove(@indices); â Clears bits at given indices.$vector->Index_List_Store(@indices); â Sets bits at given indices.@indices = $vector->Index_List_Read(); â Returns list of indices of set bits (ascending).$vec3->Or($vec1, $vec2); â Bitwise OR (union).$set3->Union($set1, $set2); â Alias.$vec3->And($vec1, $vec2); â Bitwise AND (intersection).$set3->Intersection($set1, $set2); â Alias.$vec3->AndNot($vec1, $vec2); â Bitwise AND NOT (difference).$set3->Difference($set1, $set2); â Alias.$vec3->Xor($vec1, $vec2); â Bitwise XOR (symmetric difference).$set3->ExclusiveOr($set1, $set2); â Alias.$vec2->Not($vec1); â Bitwise NOT (complement).$set2->Complement($set1); â Alias.if ($set1->subset($set2)) â Returns true if $set1 is subset of $set2.$norm = $set->Norm(); â Counts set bits (fast byte lookup).$norm = $set->Norm2(); â Counts set bits (alternative algorithm, good for sparse).$norm = $set->Norm3(); â Counts set bits (original algorithm).$min = $set->Min(); â Returns minimum index of set bit, or MAX_LONG if empty.$max = $set->Max(); â Returns maximum index of set bit, or MIN_LONG if empty.$m3->Multiplication($r3, $c3, $m1, $r1, $c1, $m2, $r2, $c2); â Boolean matrix multiplication (XOR addition).$m3->Product($r3, $c3, $m1, $r1, $c1, $m2, $r2, $c2); â Boolean matrix multiplication (OR addition).$matrix->Closure($rows, $cols); â Reflexive transitive closure (Kleene's algorithm). Matrix is modified in place.$matrix2->Transpose($rows2, $cols2, $matrix1, $rows1, $cols1); â Matrix transpose. In-place allowed only if quadratic.Bit::Vector::Overload(3), Bit::Vector::String(3), Storable(3).
Set::IntRange(3), Math::MatrixBool(3), Math::MatrixReal(3), DFA::Kleene(3), Math::Kleene(3), Graph::Kruskal(3).
This man page documents Bit::Vector version 7.4.
Steffen Beyer mailto:STBEY AT cpan.org http://www.engelschall.com/u/sb/download/
Copyright (c) 1995 - 2013 by Steffen Beyer. All rights reserved.
This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e., under the terms of the "Artistic License" or the "GNU General Public License".
The C library at the core of this Perl module can additionally be redistributed and/or modified under the terms of the "GNU Library General Public License".
Please refer to the files "Artistic.txt", "GNU_GPL.txt" and "GNU_LGPL.txt" in this distribution for details!
This package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the "GNU General Public License" for more details.
Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-12 07:53 @2600:1f28:365:80b0:6f72:a904:73b0:e266
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)