Bit::Vector - Efficient bit vector, set of integers and "big int" math library
| Use Case | Command | Description |
|---|---|---|
| Create a bit vector | Bit::Vector->new($bits) | Create a new bit vector of given size, all bits cleared |
| Set a bit | $vector->Bit_On($index) | Set a specific bit to 1 |
| Test a bit | $vector->bit_test($index) | Check if a bit is set (returns 0 or 1) |
| Convert to hex string | $vector->to_Hex() | Export as hexadecimal string |
| Add two numbers | $vec3->add($vec1,$vec2,$carry) | Add two bit vectors with carry |
| Set operations (union) | $vec3->Or($vec1,$vec2) | Compute bitwise OR (union) |
See Bit::Vector::Overload(3).
See Bit::Vector::String(3).
Version â $version = Bit::Vector->Version();Word_Bits â $bits = Bit::Vector->Word_Bits(); (bits in a machine word)Long_Bits â $bits = Bit::Vector->Long_Bits(); (bits in an unsigned long)new â $vector = Bit::Vector->new($bits); (constructor)new (multi) â @veclist = Bit::Vector->new($bits,$count);new_Hex â $vector = Bit::Vector->new_Hex($bits,$string);new_Bin â $vector = Bit::Vector->new_Bin($bits,$string);new_Dec â $vector = Bit::Vector->new_Dec($bits,$string);new_Enum â $vector = Bit::Vector->new_Enum($bits,$string);Concat_List â $vector = Bit::Vector->Concat_List(@vectors);new â $vec2 = $vec1->new($bits); (alternative call)Shadow â $vec2 = $vec1->Shadow(); (new vector, same size but empty)Clone â $vec2 = $vec1->Clone(); (exact duplicate)Concat â $vector = $vec1->Concat($vec2);Concat_List â $vector = $vec1->Concat_List($vec2,$vec3,...);Size â $bits = $vector->Size();Resize â $vector->Resize($bits);Copy â $vec2->Copy($vec1);Empty â $vector->Empty();Fill â $vector->Fill();Flip â $vector->Flip();Primes â $vector->Primes(); (Sieve of Eratosthenes)Reverse â $vec2->Reverse($vec1);Interval_Empty â $vector->Interval_Empty($min,$max);Interval_Fill â $vector->Interval_Fill($min,$max);Interval_Flip â $vector->Interval_Flip($min,$max);Interval_Reverse â $vector->Interval_Reverse($min,$max);Interval_Scan_inc â if (($min,$max) = $vector->Interval_Scan_inc($start))Interval_Scan_dec â if (($min,$max) = $vector->Interval_Scan_dec($start))Interval_Copy â $vec2->Interval_Copy($vec1,$offset2,$offset1,$length);Interval_Substitute â $vec2->Interval_Substitute($vec1,$off2,$len2,$off1,$len1);is_empty â if ($vector->is_empty())is_full â if ($vector->is_full())equal â if ($vec1->equal($vec2))Lexicompare (unsigned) â if ($vec1->Lexicompare($vec2) == 0) etc.Compare (signed) â if ($vec1->Compare($vec2) == 0) etc.to_Hex â $string = $vector->to_Hex();from_Hex â $vector->from_Hex($string);to_Bin â $string = $vector->to_Bin();from_Bin â $vector->from_Bin($string);to_Dec â $string = $vector->to_Dec();from_Dec â $vector->from_Dec($string);to_Enum â $string = $vector->to_Enum(); (e.g. "2,3,5-7,11,13-19")from_Enum â $vector->from_Enum($string);Bit_Off â $vector->Bit_Off($index);Bit_On â $vector->Bit_On($index);bit_flip â $bit = $vector->bit_flip($index);bit_test / contains â $bit = $vector->bit_test($index);Bit_Copy â $vector->Bit_Copy($index,$bit);LSB â $vector->LSB($bit);MSB â $vector->MSB($bit);lsb â $bit = $vector->lsb();msb â $bit = $vector->msb();rotate_left â $carry = $vector->rotate_left();rotate_right â $carry = $vector->rotate_right();shift_left â $carry = $vector->shift_left($carry);shift_right â $carry = $vector->shift_right($carry);Move_Left â $vector->Move_Left($bits);Move_Right â $vector->Move_Right($bits);Insert â $vector->Insert($offset,$bits);Delete â $vector->Delete($offset,$bits);increment â $carry = $vector->increment();decrement â $carry = $vector->decrement();inc â $overflow = $vec2->inc($vec1);dec â $overflow = $vec2->dec($vec1);add â $carry = $vec3->add($vec1,$vec2,$carry);subtract â $carry = $vec3->subtract($vec1,$vec2,$carry);Neg / Negate â $vec2->Neg($vec1);Abs / Absolute â $vec2->Abs($vec1);Sign â if ($vector->Sign() == 0)Multiply â $vec3->Multiply($vec1,$vec2);Divide â $quot->Divide($vec1,$vec2,$rest);GCD â $vecgcd->GCD($veca,$vecb);Power â $vec3->Power($vec1,$vec2);Block_Store â $vector->Block_Store($buffer);Block_Read â $buffer = $vector->Block_Read();Word_Size â $size = $vector->Word_Size();Word_Store â $vector->Word_Store($offset,$word);Word_Read â $word = $vector->Word_Read($offset);Word_List_Store â $vector->Word_List_Store(@words);Word_List_Read â @words = $vector->Word_List_Read();Word_Insert â $vector->Word_Insert($offset,$count);Word_Delete â $vector->Word_Delete($offset,$count);Chunk_Store â $vector->Chunk_Store($chunksize,$offset,$chunk);Chunk_Read â $chunk = $vector->Chunk_Read($chunksize,$offset);Chunk_List_Store â $vector->Chunk_List_Store($chunksize,@chunks);Chunk_List_Read â @chunks = $vector->Chunk_List_Read($chunksize);Index_List_Remove â $vector->Index_List_Remove(@indices);Index_List_Store â $vector->Index_List_Store(@indices);Index_List_Read â @indices = $vector->Index_List_Read();Or / Union â $vec3->Or($vec1,$vec2);And / Intersection â $vec3->And($vec1,$vec2);AndNot / Difference â $vec3->AndNot($vec1,$vec2);Xor / ExclusiveOr â $vec3->Xor($vec1,$vec2);Not / Complement â $vec2->Not($vec1);subset â if ($set1->subset($set2))Norm â $norm = $set->Norm();Min â $min = $set->Min();Max â $max = $set->Max();Multiplication â $matrix3->Multiplication(...);Product â $matrix3->Product(...);Closure â $matrix->Closure($rows,$cols);Transpose â $matrix2->Transpose(...);new()).Block_Read()/Block_Store() use "least order byte first" order for portability.Word_ are MACHINE-DEPENDENT. Use Chunk_ methods for portability (chunk sizes up to 32 bits).Bit::Vector->Long_Bits() bits. For portability, use ⤠32 bits.Concat(), Concat_List(), Copy(), Interval_Copy(), Interval_Substitute(), Multiply(), Power().$vector->Size()-1.Storable automatically. See Storable(3).See Bit::Vector::Overload(3).
See Bit::Vector::String(3).
$version = Bit::Vector->Version(); â Returns the current version number.$bits = Bit::Vector->Word_Bits(); â Returns the number of bits in an unsigned int (machine word).$bits = Bit::Vector->Long_Bits(); â Returns the number of bits in an unsigned long.$vector = Bit::Vector->new($bits); â Constructor. Creates a bit vector of size $bits (all bits cleared). Exception if memory allocation fails. Negative $bits interpreted as large positive.@veclist = Bit::Vector->new($bits,$count); â Creates multiple bit vectors at once. Returns a list of $count vectors.$vector = Bit::Vector->new_Hex($bits,$string); â Alternative constructor: creates and initializes from hex string.$vector = Bit::Vector->new_Bin($bits,$string); â Alternative constructor: creates and initializes from binary string.$vector = Bit::Vector->new_Dec($bits,$string); â Alternative constructor: creates and initializes from decimal string.$vector = Bit::Vector->new_Enum($bits,$string); â Alternative constructor: creates and initializes from enumeration string.$vector = Bit::Vector->Concat_List(@vectors); â Creates a new vector by concatenating all given vectors. Rightmost vector becomes least significant.$vec2 = $vec1->new($bits); â Alternative way to call constructor (using an existing vector as anchor).$vec2 = $vec1->Shadow(); â Creates a new empty vector of same size as $vec1.$vec2 = $vec1->Clone(); â Creates an exact copy of $vec1.$vector = $vec1->Concat($vec2); â Returns concatenation of $vec1 (most significant) and $vec2 (least significant).$vector = $vec1->Concat_List($vec2,$vec3,...); â Concatenates multiple vectors.$bits = $vector->Size(); â Returns the size (number of bits) of the vector.$vector->Resize($bits); â Changes the size of the vector, preserving as many bits as fit. May allocate new memory if needed.$vec2->Copy($vec1); â Copies contents of $vec1 to $vec2. If source is larger, truncates; if target is larger, sign-extends.$vector->Empty(); â Clears all bits.$vector->Fill(); â Sets all bits.$vector->Flip(); â Flips (complements) all bits.$vector->Primes(); â Sets all bits whose indices are prime numbers (Sieve of Eratosthenes).$vec2->Reverse($vec1); â Reverses order of bits (LSB â MSB).$vector->Interval_Empty($min,$max); â Clears bits in interval [$min, $max].$vector->Interval_Fill($min,$max); â Sets bits in interval.$vector->Interval_Flip($min,$max); â Flips bits in interval.$vector->Interval_Reverse($min,$max); â Reverses order of bits in interval.if (($min,$max) = $vector->Interval_Scan_inc($start)) â Finds next contiguous block of set bits scanning upward from $start. Returns empty list if none.if (($min,$max) = $vector->Interval_Scan_dec($start)) â Same, scanning downward.$vec2->Interval_Copy($vec1,$offset2,$offset1,$length); â Copies a stretch of bits from $vec1 to $vec2. Handles overlapping.$vec2->Interval_Substitute($vec1,$off2,$len2,$off1,$len1); â Replaces a stretch in $vec2 with a stretch from $vec1. Automatically resizes target vector. Similar to splice.if ($vector->is_empty()) â Returns true if all bits are cleared (zero).if ($vector->is_full()) â Returns true if all bits are set (minus one).if ($vec1->equal($vec2)) â Returns true if vectors are identical.$cmp = $vec1->Lexicompare($vec2); â Compares as unsigned numbers: returns -1, 0, or 1.$cmp = $vec1->Compare($vec2); â Compares as signed numbers (two's complement).$string = $vector->to_Hex(); â Returns hexadecimal string (LSB at right).$vector->from_Hex($string); â Reads from hex string. Ignores extra digits; clears remaining bits.$string = $vector->to_Bin(); â Returns binary string (LSB at right).$vector->from_Bin($string); â Reads from binary string.$string = $vector->to_Dec(); â Converts to decimal string (signed). Slow; use sparingly.$vector->from_Dec($string); â Converts decimal string to bit vector. Supports negative numbers. Throws overflow error if too large.$string = $vector->to_Enum(); â Returns enumeration of set bits (e.g., "2,3,5-7").$vector->from_Enum($string); â Sets bits from enumeration string.$vector->Bit_Off($index); â Clears a single bit.$vector->Bit_On($index); â Sets a single bit.$bit = $vector->bit_flip($index); â Flips a bit and returns new state.$bit = $vector->bit_test($index); â Returns state of a bit (0 or 1).$vector->Bit_Copy($index,$bit); â Sets a bit to given boolean value.$vector->LSB($bit); â Sets the least significant bit.$vector->MSB($bit); â Sets the most significant bit.$bit = $vector->lsb(); â Returns least significant bit.$bit = $vector->msb(); â Returns most significant bit.$carry = $vector->rotate_left(); â Rotates left, returns carry out.$carry = $vector->rotate_right(); â Rotates right.$carry = $vector->shift_left($carry_in); â Shifts left, shifting in $carry_in.$carry = $vector->shift_right($carry_in); â Shifts right.$vector->Move_Left($bits); â Moves (shifts) left by $bits positions, losing upper bits.$vector->Move_Right($bits); â Moves right by $bits positions, losing lower bits.$vector->Insert($offset,$bits); â Inserts $bits zero bits at $offset, shifting up, losing upper bits.$vector->Delete($offset,$bits); â Deletes $bits bits at $offset, shifting down, clearing upper bits.$carry = $vector->increment(); â Increments as unsigned. Returns carry if overflow.$carry = $vector->decrement(); â Decrements as unsigned.$overflow = $vec2->inc($vec1); â Copies and increments, returns overflow flag.$overflow = $vec2->dec($vec1); â Copies and decrements.$carry = $vec3->add($vec1,$vec2,$carry); â Adds with carry. In list context returns (carry, overflow).$carry = $vec3->subtract($vec1,$vec2,$carry); â Subtracts with carry.$vec2->Neg($vec1); â Two's complement (negate).$vec2->Abs($vec1); â Absolute value.$sign = $vector->Sign(); â Returns 0 (zero), 1 (positive), or -1 (negative).$vec3->Multiply($vec1,$vec2); â Signed multiplication. Result may be larger; overflow if too small.$quot->Divide($vec1,$vec2,$rest); â Signed division. Returns quotient and remainder.$vecgcd->GCD($veca,$vecb); â Greatest common divisor (Euclid's algorithm).$vecgcd->GCD($vecx,$vecy,$veca,$vecb); â Extended GCD: also returns coefficients x and y such that GCD = x*a + y*b.$vec3->Power($vec1,$vec2); â Exponentiation (base^exponent). Efficient divide-and-conquer.$vector->Block_Store($buffer); â Loads bit vector from a binary string (low-order byte first).$buffer = $vector->Block_Read(); â Exports bit vector as binary string.$size = $vector->Word_Size(); â Returns number of machine words in the internal representation.$vector->Word_Store($offset,$word); â Stores a machine word at given offset.$word = $vector->Word_Read($offset); â Reads a machine word.$vector->Word_List_Store(@words); â Stores list of words (least significant first).@words = $vector->Word_List_Read(); â Returns list of words.$vector->Word_Insert($offset,$count); â Inserts words at offset.$vector->Word_Delete($offset,$count); â Deletes 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); â Fills vector with list of chunks.@chunks = $vector->Chunk_List_Read($chunksize); â Returns list of chunks. Caution: can be huge.$vector->Index_List_Remove(@indices); â Clears specified bits.$vector->Index_List_Store(@indices); â Sets specified bits.@indices = $vector->Index_List_Read(); â Returns list of indices of set bits.$vec3->Or($vec1,$vec2); â Bitwise OR (union of sets).$vec3->And($vec1,$vec2); â Bitwise AND (intersection).$vec3->AndNot($vec1,$vec2); â Bitwise AND NOT (set difference).$vec3->Xor($vec1,$vec2); â Bitwise XOR (symmetric difference).$vec2->Not($vec1); â Bitwise NOT (complement).if ($set1->subset($set2)) â Returns true if $set1 is subset of $set2.$norm = $set->Norm(); â Returns number of set bits (norm).$norm = $set->Norm2(); â Alternative norm calculation (may be faster for sparse sets).$norm = $set->Norm3(); â Another norm method (slowest on average).$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 using Kleene's algorithm.$matrix2->Transpose($rows2,$cols2,$matrix1,$rows1,$cols1); â Matrix transpose.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.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-21 05:39 @216.73.216.45
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)