Bit::Ve...Overload(3)
π
Bit::Vector::Overload (3) β User Contributed Perl Documentation
okay bestie, so this page is from the User Contributed Perl Documentation, section 3, and it's literally all about Bit::Vector::Overload. grab a strawberry drink π and let's go.
β¨ NAME β¨
Bit::Vector::Overload β the overloaded-operators add-on for Bit::Vector. that's it, that's the name, she's iconic π
π«Ά USAGE π«Ά
okay so first thing, real talk: you do not need to "use Bit::Vector;" on top of this module. like, don't do both, that's not the vibe.
just "use Bit::Vector::Overload;" INSTEAD of "use Bit::Vector;". that's the whole move π
and no, you're not losing anything β you can still use literally all the methods from the Bit::Vector module, plus the overloaded operators and methods provided here, after that. best of both worlds fr fr.
π« SYNOPSIS π«
here's the whole mood board of what you can do, grouped by vibe π
Configuration
$config = Bit::Vector->Configuration();
Bit::Vector->Configuration($config);
$oldconfig = Bit::Vector->Configuration($newconfig);
String Conversion
$string = "$vector"; # depending on configuration
print "\$vector = '$vector'\n";
Emptyness
if ($vector) # if not empty (non-zero)
if (! $vector) # if empty (zero)
unless ($vector) # if empty (zero)
Complement (one's complement)
$vector2 = ~$vector1;
$vector = ~$vector;
Negation (two's complement)
$vector2 = -$vector1;
$vector = -$vector;
Norm
$norm = abs($vector); # depending on configuration
Absolute
$vector2 = abs($vector1); # depending on configuration
Concatenation
$vector3 = $vector1 . $vector2;
$vector1 .= $vector2;
$vector1 = $vector2 . $vector1;
$vector2 = $vector1 . $scalar; # depending on configuration
$vector2 = $scalar . $vector1;
$vector .= $scalar;
Duplication
$vector2 = $vector1 x $factor;
$vector x= $factor;
Shift Left
$vector2 = $vector1 << $bits;
$vector <<= $bits;
Shift Right
$vector2 = $vector1 >> $bits;
$vector >>= $bits;
Union
$vector3 = $vector1 | $vector2;
$vector1 |= $vector2;
$vector2 = $vector1 | $scalar;
$vector |= $scalar;
$vector3 = $vector1 + $vector2; # depending on configuration
$vector1 += $vector2;
$vector2 = $vector1 + $scalar;
$vector += $scalar;
Intersection
$vector3 = $vector1 & $vector2;
$vector1 &= $vector2;
$vector2 = $vector1 & $scalar;
$vector &= $scalar;
$vector3 = $vector1 * $vector2; # depending on configuration
$vector1 *= $vector2;
$vector2 = $vector1 * $scalar;
$vector *= $scalar;
ExclusiveOr
$vector3 = $vector1 ^ $vector2;
$vector1 ^= $vector2;
$vector2 = $vector1 ^ $scalar;
$vector ^= $scalar;
Set Difference
$vector3 = $vector1 - $vector2; # depending on configuration
$vector1 -= $vector2;
$vector1 = $vector2 - $vector1;
$vector2 = $vector1 - $scalar;
$vector2 = $scalar - $vector1;
$vector -= $scalar;
Addition
$vector3 = $vector1 + $vector2; # depending on configuration
$vector1 += $vector2;
$vector2 = $vector1 + $scalar;
$vector += $scalar;
Subtraction
$vector3 = $vector1 - $vector2; # depending on configuration
$vector1 -= $vector2;
$vector1 = $vector2 - $vector1;
$vector2 = $vector1 - $scalar;
$vector2 = $scalar - $vector1;
$vector -= $scalar;
Multiplication
$vector3 = $vector1 * $vector2; # depending on configuration
$vector1 *= $vector2;
$vector2 = $vector1 * $scalar;
$vector *= $scalar;
Division
$vector3 = $vector1 / $vector2;
$vector1 /= $vector2;
$vector1 = $vector2 / $vector1;
$vector2 = $vector1 / $scalar;
$vector2 = $scalar / $vector1;
$vector /= $scalar;
Modulo
$vector3 = $vector1 % $vector2;
$vector1 %= $vector2;
$vector1 = $vector2 % $vector1;
$vector2 = $vector1 % $scalar;
$vector2 = $scalar % $vector1;
$vector %= $scalar;
Exponentiation
$vector3 = $vector1 ** $vector2;
$vector1 **= $vector2;
$vector2 = $vector1 ** $scalar;
$vector2 = $scalar ** $vector1;
$vector **= $scalar;
Increment
++$vector;
$vector++;
Decrement
--$vector;
$vector--;
Lexical Comparison (unsigned)
$cmp = $vector1 cmp $vector2;
if ($vector1 lt $vector2)
if ($vector1 le $vector2)
if ($vector1 gt $vector2)
if ($vector1 ge $vector2)
$cmp = $vector cmp $scalar;
if ($vector lt $scalar)
if ($vector le $scalar)
if ($vector gt $scalar)
if ($vector ge $scalar)
Comparison (signed)
$cmp = $vector1 <=> $vector2;
if ($vector1 < $vector2) # depending on configuration
if ($vector1 <= $vector2)
if ($vector1 > $vector2)
if ($vector1 >= $vector2)
$cmp = $vector <=> $scalar;
if ($vector < $scalar) # depending on configuration
if ($vector <= $scalar)
if ($vector > $scalar)
if ($vector >= $scalar)
Equality
if ($vector1 eq $vector2)
if ($vector1 ne $vector2)
if ($vector eq $scalar)
if ($vector ne $scalar)
if ($vector1 == $vector2)
if ($vector1 != $vector2)
if ($vector == $scalar)
if ($vector != $scalar)
Subset Relationship
if ($vector1 <= $vector2) # depending on configuration
True Subset Relationship
if ($vector1 < $vector2) # depending on configuration
Superset Relationship
if ($vector1 >= $vector2) # depending on configuration
True Superset Relationship
if ($vector1 > $vector2) # depending on configuration
π₯ IMPORTANT NOTES π₯
okay this part is the "listen to me carefully bestie" part, don't skip it π
- Boolean values β in this module, booleans are always a numeric zero (
0) for "false" and a numeric one (1) for "true". no vibes, no truthiness, just0and1, periodt. - Negative numbers β numeric factors (the ones you need for the
"<<",">>"and"x"operators) and bit numbers are always regarded as being UNSIGNED. ngl this one bites people.
so like, the consequence: whenever you pass a negative number for such a factor or bit number, it's gonna get treated as a (usually very large) positive number thanks to its internal two's complement binary representation. which usually = malfunctions, or an "index out of range" error message and then your program just β¨ dies β¨ π
heads up: this does not apply to "big integer" decimal numbers, which are (usually) passed as strings, and which may of course be negative (peep the section "Big integers" a little further below π).
- Overloaded operators configuration β the behaviour of certain overloaded operators can be changed in various ways via the
"Configuration()"method (more details in that method's description further below).
for instance: scalars (i.e., numbers and strings) handed to overloaded operators as operands get automatically converted to bit vectors, internally.
and those scalars are thereby automatically assumed to be indices, or to be in hexadecimal, binary, decimal or enumeration format β depending on the configuration.
same energy for the other direction: when converting bit vectors to strings using double quotes (""), the output format also depends on the configuration you picked earlier.
and finally, some overloaded operators may have different semantics depending on the proper configuration β for example, the operator "+" can be the "union" operator from set theory, or the arithmetic "add" operator. two completely different girlies, same symbol π
in all cases (input, output and operator semantics), the defaults were chosen in such a way that the module's behaviour is backward compatible with previous versions. we love a considerate icon π
- "Big integers" β as long as "big integers" (for "big integer" arithmetic) are small enough that Perl doesn't need scientific notation (exponents) to represent them internally, you can hand these "big integer" constants to this module's overloaded operators (or to the method
"from_Dec()") in numeric form β i.e., either as a numeric constant or expression, or as a Perl variable containing a numeric value.
warning bestie: you will get an error message (resulting in program abortion π) if your "big integer" numbers exceed that limit.
and because that limit is machine-dependent and genuinely not obvious to find out, it is strongly recommended that you enclose ALL your "big integer" constants in your programs in (double or single) quotes. just do it, future you will thank you π«Ά
Examples:
$vector /= 10; # ok because number is small
$vector /= -10; # ok for same reason
$vector /= "10"; # always correct
$vector += "1152921504606846976"; # quotes probably required here
All examples assume
Bit::Vector->Configuration("input=decimal");
having been set beforehand.
also note: this module does not support scientific notation (exponents) for "big integer" decimal numbers β because you can always make the bit vector large enough for the whole number to fit without loss of precision (which is what would happen if scientific notation were used). so like, just make it bigger, no cap.
and finally (last one, promise π): the only characters allowed in "big integer" constant strings are the digits 0..9 and an optional leading sign ("+" or "-").
all other characters produce a syntax error. sorry babe, rules are rules.
- Valid operands for overloaded operators β all overloaded operators expect at least one bit vector operand, so that the operator can "know" it's not the usual operation being carried out, but rather the overloaded variant. it's giving self-awareness β¨
this is especially true for all unary operators:
"$vector"
if ($vector)
if (!$vector)
~$vector
-$vector
abs($vector)
++$vector
$vector++
--$vector
$vector--
and for obvious reasons, the left operand (the "lvalue") of all assignment operators is also required to be a bit vector:
.=
x=
<<=
>>=
|=
&=
^=
+=
-=
*=
/=
%=
**=
Okay so for three special operators β namely <<, >> and x, plus their assignment variants <<=, >>= and x= β the left operand is ALWAYS a bit vector and the right operand is ALWAYS a number (the factor saying how many times the operator gets applied). No cap.
For every truly binary operator, aka these:
.
|
&
^
+
-
*
/
%
**
<=> cmp
== eq
!= ne
< lt
<= le
> gt
>= ge
one of the two operands can be replaced by a Perl scalar β a number or a string, as a Perl constant, a Perl expression, or a Perl variable that yields a number or a string. Slay.
Same deal for the right side operand (the rvalue) of the remaining assignment operators, i.e.:
.=
|=
&=
^=
+=
-=
*=
/=
%=
**=
Heads up, bestie:</p> this Perl scalar should be the correct type β numeric or string β for your chosen configuration. Otherwise a warning message will occur if your program runs under the "-w" switch of Perl. iykyk.
The acceptable scalar types for each possible configuration are:
input = bit indices (default) : numeric
input = hexadecimal : string
input = binary : string
input = decimal : string (in general)
input = decimal : numeric (if small enough)
input = enumeration : string
NOTE ALSO THAT THESE SCALAR OPERANDS ARE CONVERTED TO BIT VECTORS OF THE SAME SIZE AS THE BIT VECTOR WHICH IS THE OTHER OPERAND. π
The only exception from this rule is the concatenation operator (".") and its assignment variant (".=") β¨ iconique β¨:
If one of the two operands of the concatenation operator (".") is not a bit vector object but a Perl scalar, the contents of the remaining bit vector operand are converted into a string (the format depends on the configuration set with the Configuration() method), which is then concatenated in the proper order (i.e., as indicated by the order of the two operands) with the Perl scalar. In other words, a string is returned in such a case instead of a bit vector object! π«Ά
If the right side operand (the rvalue) of the assignment variant (".=") of the concatenation operator is a Perl scalar, it is converted internally to a bit vector of the same size as the left side operand β provided that the configuration states that scalars are to be regarded as indices, decimal strings or enumerations.
If the configuration states that scalars are to be regarded as hexadecimal or boolean strings, however, these strings are converted to bit vectors of a size matching the length of the input string β i.e., four times the length for hexadecimal strings (because each hexadecimal digit is worth 4 bits) and once the length for binary strings.
If a decimal number (big integer) is too large to be stored in a bit vector of the given size, a "numeric overflow error" occurs. π
If a bit index is out of range for the given bit vector, an "index out of range" error occurs.
If a scalar operand cannot be converted successfully due to invalid syntax, a fatal "input string syntax error" is issued.
If the two operands of the operator <<, >> or x are reversed, a fatal "reversed operands error" occurs.
If an operand is neither a bit vector nor a scalar, then a fatal "illegal operand type error" occurs.
π Bit order π
Bit vectors are stored least order bit and least order word first internally. Thatβs the vibe internally.
So bit #0 of any given bit vector corresponds to bit #0 of word #0 in the array of machine words representing the bit vector.
(Where word #0 comes first in memory, i.e., it is stored at the least memory address in the allocated block of memory holding the given bit vector.)
But note: machine words can be stored least order byte first or last, depending on your systemβs implementation.
Also note that whenever bit vectors are converted to and from (binary or hexadecimal) strings, the RIGHTMOST bit is always the LEAST SIGNIFICANT one, and the LEFTMOST bit is always the MOST SIGNIFICANT bit.
This is because in our western culture, numbers are always represented in this way (least significant to most significant digits go from right to left).
Of course this requires an internal reversion of order, which the corresponding conversion methods perform automatically β without any additional overhead, itβs just a matter of starting the internal loop at the bottom or the top end. Periodt.
π§© Matching sizes π§©
In general, for methods involving several bit vectors at the same time, all bit vector arguments must have identical sizes (number of bits), or a fatal "size mismatch" error will occur. β οΈ
Exceptions from this rule are the methods Concat(), Concat_List(), Copy(), Interval_Copy() and Interval_Substitute(), where no conditions at all are imposed on the size of their bit vector arguments.
In method Multiply(), all three bit vector arguments must in principle obey the rule of matching sizes, but the bit vector in which the result of the multiplication is to be stored may be larger than the two bit vector arguments containing the factors for the multiplication.
In method Power(), the bit vector for the result must be the same size or greater than the base of the exponentiation term. The exponent can be any size.
The same applies to the corresponding overloaded operators.
π¦ Index ranges π¦
All indices for any given bits must lie between 0 and $vector->Size()-1, or a fatal "index out of range" error will occur. No sneaky out-of-bounds behavior, bestie.
π DESCRIPTION π
$config = Bit::Vector->Configuration();Bit::Vector->Configuration($config);$oldconfig = Bit::Vector->Configuration($newconfig);
This method serves to alter the semantics (i.e., behaviour) of certain overloaded operators β which are all implemented in Perl, by the way. β¨
It does not have any effect whatsoever on anything else. In particular, it does not affect the methods implemented in C.
The method accepts an (optional) string as input in which certain keywords are expected, which influence some or almost all of the overloaded operators in several possible ways.
The method always returns a string (which you do not need to take care of, i.e., to store, in case you arenβt interested in keeping it) which is a complete representation of the current configuration (i.e., BEFORE any modifications are applied) and which can be fed back to this method later in order to restore the previous configuration.
There are three aspects of the way certain overloaded operators behave which can be controlled with this method:
- the way scalar operands (replacing one of the two bit vector object operands) are automatically converted internally into a bit vector object of their own,
- the operation certain overloaded operators perform, i.e., an operation with sets or an arithmetic operation,
- the format to which bit vectors are converted automatically when they are enclosed in double quotes.
The input string may contain any number of assignments, each of which controls one of these three aspects.
Each assignment has the form "<which>=<value>".
"<which>" and "<value>" thereby consist of letters ([a-zA-Z]) and white space.
Multiple assignments have to be separated by one or more comma (,), semi-colon (;), colon (:), vertical bar (|), slash (/), newline (\n), ampersand (&), plus (+) or dash (-).
Empty lines or statements (only white space) are allowed but will be ignored.
"<which>" has to contain one or more keywords from one of three groups, each group representing one of the three aspects that the Configuration() method controls:
^scalar,^input,^in$^operator,^semantic,^ops$^string,^output,^out$
The character ^ thereby denotes the beginning of a word, and $ denotes the end. Case is ignored (!). Yes, really.
Using these keywords, you can build any phrase you like to select one of the three aspects (see also examples given below).
The only condition is that no other keyword from any of the other two groups may match β otherwise a syntax error will occur (i.e., ambiguities are forbidden). A syntax error also occurs if none of the keywords matches.
This same principle applies to "<value>":
Depending on which aspect you specified for "<which>", there are different groups of keywords that determine the value the selected aspect will be set to:
<which>=^scalar,^input,^in$:<value>=^bit$,^index,^indice^hex^bin^dec^enum
<which>=^operator,^semantic,^ops$:<value>=^set$^arithmetic
<which>=^string,^output,^out$:<value>=^hex^bin^dec^enum
Examples:
"Any scalar input I provide should be considered to be = a bit index"
"I want to have operator semantics suitable for = arithmetics"
"Any bit vector in double quotes is to be output as = an enumeration"
πΈ SCALAR INPUT πΈ
In the case of scalar input, ^bit$, ^index, or ^indice all cause scalar input to be considered to represent a bit index, i.e., $vector ^= 5; will flip bit #5 in the given bit vector (this is essentially the same as $vector->bit_flip(5);).
Note that βbit indicesβ is the default setting for βscalar inputβ. π
The keyword "^hex" makes scalar input get treated as hexadecimal, so $vector ^= 5; flips bit #0 and bit #2 (because hexadecimal 5 is binary 0101).
β οΈ Bestie, hexadecimal input should always be wrapped in quotes β otherwise Perl will read it as a decimal number instead! That example only works because hexadecimal0-9and decimal0-9are literally the same characters. iykyk π
The keyword "^bin" makes scalar input get treated as binary format. In this case, every character except 0 and 1 is forbidden β and yes, that means it produces a syntax error. So $vector ^= '0101';, for example, flips bit #0 and bit #2. β¨
The keyword "^dec" makes scalar input get treated as decimal integers, meaning $vector ^= 5; flips bit #0 and bit #2 (because decimal 5 is binary 0101).
β οΈ And all decimal input should be quoted too, because for large numbers Perl will use scientific notation internally to represent them β which produces a syntax error, since scientific notation is neither supported by this module nor needed. π«Ά
Finally, the keyword "^enum" makes scalar input get treated as a list (βenumerationβ) of indices and ranges of contiguous indices. So $vector |= '2,3,5,7-13,17-23'; sets bits #2, #3, #5, #7 through #13, and #17 through #23. Slay. π
π OPERATOR SEMANTICS π
Several overloaded operators can have two totally distinct functions depending on this setting.
The affected operators are: +, -, *, <, <=, > and >=.
With the default setting, βset operationsβ, these operators perform:
+ set union ( set1 u set2 )
- set difference ( set1 \ set2 )
* set intersection ( set1 n set2 )
< true subset relationship ( set1 < set2 )
<= subset relationship ( set1 <= set2 )
> true superset relationship ( set1 > set2 )
>= superset relationship ( set1 >= set2 )
With the alternative setting, βarithmetic operationsβ, these operators perform:
+ addition ( num1 + num2 )
- subtraction ( num1 - num2 )
* multiplication ( num1 * num2 )
< "less than" comparison ( num1 < num2 )
<= "less than or equal" comparison ( num1 <= num2 )
> "greater than" comparison ( num1 > num2 )
>= "greater than or equal" comparison ( num1 >= num2 )
Note that these latter comparison operators (<, <=, > and >=) regard their operands as being SIGNED. π₯
To perform comparisons with UNSIGNED operands, use the operators lt, le, gt and ge instead β in contrast to the operators above, these are NOT affected by the βoperator semanticsβ setting. No cap. π
π¦ STRING OUTPUT π¦
There are four methods that convert the contents of a given bit vector into a string: to_Hex(), to_Bin(), to_Dec() and to_Enum() β not counting Block_Read(), since that method does not return a human-readable string. π
(For conversion to octal, see the description of the method Chunk_List_Read().)
So there are four possible formats a bit vector can be converted into when itβs enclosed in double quotes, for example:
print "\$vector = '$vector'\n";
$string = "$vector";
That means you can set βstring outputβ to four different values: hex for hexadecimal format (which is the default), bin for binary format, dec for conversion to decimal numbers, and enum for conversion to enumerations (".newsrc" style sets). β¨
β οΈ BEWARE: conversion to decimal numbers is inherently slow β it can easily take up several seconds for a single large bit vector! π
So you should store the decimal strings returned to you rather than converting a given bit vector again. Periodt. π
πΈ EXAMPLES πΈ
The default setting as returned by the method Configuration() is:
Scalar Input = Bit Index
Operator Semantics = Set Operators
String Output = Hexadecimal
Performing a statement such as:
Bit::Vector->Configuration("in=bin,ops=arithmetic,out=bin");
print Bit::Vector->Configuration(), "\n";
yields the following output:
Scalar Input = Binary
Operator Semantics = Arithmetic Operators
String Output = Binary
Note that you can always feed this output back into the Configuration() method to restore that setting later. π«Ά
This also means you can enter the same setting with almost any degree of verbosity you like β as long as the required keywords appear and no ambiguities arise. π
Note further that any aspect you do not specify is not changed, i.e., the statement
Bit::Vector->Configuration("operators = arithmetic");
leaves all other aspects unchanged.
"$vector"β Remember that variables enclosed in double quotes are always interpolated in Perl. Whenever a Perl variable containing the reference of aBit::Vectorobject is enclosed in double quotes (either alone or together with other text and/or variables), the contents of the corresponding bit vector are converted into a printable string. Since there are several conversion methods available in this module (see the description of the methodsto_Hex(),to_Bin(),to_Dec()andto_Enum()), it is of course desirable to be able to choose which of these methods gets applied in this case. This can actually be done by changing the configuration of this module using the methodConfigure()(see the previous chapter, immediately above). The default is conversion to hexadecimal. πif ($vector)β It is possible to use a Perl variable containing the reference of aBit::Vectorobject as a boolean expression. The condition above is true if the corresponding bit vector contains at least one set bit, and it is false if ALL bits of the corresponding bit vector are cleared. β¨if (!$vector)β Since itβs possible to use a Perl variable containing the reference of aBit::Vectorobject as a boolean expression, you can of course also negate this boolean expression. The condition above is true if ALL bits of the corresponding bit vector are cleared, and it is false if the corresponding bit vector contains at least one set bit. Note that this is NOT the same as using the methodis_full(), which returns true if ALL bits of the corresponding bit vector are SET. π~$vectorβ This term returns a new bit vector object which is the oneβs complement of the given bit vector. This is equivalent to inverting all bits. π¦-$vector(unary minus) β This term returns a new bit vector object which is the twoβs complement of the given bit vector. This is equivalent to inverting all bits and incrementing the result by one. (This is the same as changing the sign of a number in twoβs complement binary representation.) π₯abs($vector)β Depending on the configuration (see the description of the methodConfiguration()for more details), this term either returns the number of set bits in the given bit vector (this is the same as calculating the number of elements which are contained in the given set) β which is the default behaviour β or it returns a new bit vector object which contains the absolute value of the number stored in the given bit vector. π"$vector1 . $vector2"β This term usually returns a new bit vector object which is the result of the concatenation of the two bit vector operands. The left operand becomes the most significant, and the right operand becomes the least significant part of the new bit vector object. If one of the two operands is not a bit vector object but a Perl scalar, however, the contents of the remaining bit vector operand are converted into a string (the format of which depends on the configuration set with theConfiguration()method), which is then concatenated in the proper order (i.e., as indicated by the order of the two operands) with the Perl scalar. In other words, a string is returned in such a case instead of a bit vector object! π"$vector x $factor"β This term returns a new bit vector object which is the concatenation of as many copies of the given bit vector operand (the left operand) as the factor (the right operand) specifies. If the factor is zero, a bit vector object with a length of zero bits is returned. If the factor is one, just a new copy of the given bit vector is returned. Note that a fatal βreversed operands errorβ occurs if the two operands are swapped. π"$vector << $bits"β This term returns a new bit vector object which is a copy of the given bit vector (the left operand), which is then shifted left (towards the most significant bit) by as many places as the right operand,$bits, specifies. This means that the$bitsmost significant bits are lost, all other bits move up by$bitspositions, and the$bitsleast significant bits that have been left unoccupied by this shift are all set to zero. If$bitsis greater than the number of bits of the given bit vector, this term returns an empty bit vector (i.e., with all bits cleared) of the same size as the given bit vector. Note that a fatal βreversed operands errorβ occurs if the two operands are swapped. π"$vector >> $bits"β This term returns a new bit vector object which is a copy of the given bit vector (the left operand), which is then shifted right (towards the least significant bit) by as many places as the right operand,$bits, specifies. This means that the$bitsleast significant bits are lost, all other bits move down by$bitspositions, and the$bitsmost significant bits that have been left unoccupied by this shift are all set to zero. If$bitsis greater than the number of bits of the given bit vector, this term returns an empty bit vector (i.e., with all bits cleared) of the same size as the given bit vector. Note that a fatal βreversed operands errorβ occurs if the two operands are swapped. π«Ά"$vector1 | $vector2"β This term returns a new bit vector object which is the result of a bitwise OR operation between the two bit vector operands. π$vector1 & $vector2β new bit vector object, result of a bitwise AND between the two operands. It's giving intersection of two sets π$vector1 ^ $vector2β new bit vector object, result of a bitwise XOR (exclusive-or) between the two operands. That's the symmetric difference of two sets, no cap β¨$vector1 + $vector2β okay so this one is config-dependent (peep the description of the methodConfiguration()for the deets). Either it returns a new bit vector object that's the bitwise OR of the two operands (same as the union of two sets) β and that's the default behaviour β or it returns a new bit vector object holding the sum of the two numbers stored in the two operands. Choose your fighter π«Ά$vector1 - $vector2β again, depends on configuration (see the methodConfiguration()description for more). Either it returns a new bit vector object that's the set difference of the two sets represented in the two operands β default behaviour, bestie β or it returns a new bit vector object holding the difference of the two numbers stored in the two operands π$vector1 * $vector2β config check again (Configuration(), you know the drill). Either a new bit vector object with the bitwise AND of the two operands (i.e. the intersection of two sets) β that's the default β or a new bit vector object with the product of the two numbers stored in the two operands πΈ$vector1 / $vector2β new bit vector object containing the result of the division of the two numbers stored in the two operands π$vector1 % $vector2β new bit vector object containing the remainder of the division of the two numbers stored in the two operands β¨$vector1 ** $vector2β new bit vector object containing the result of the exponentiation of the left operand elevated to the right operand's power π₯$vector1 .= $vector2;β this statement "appends" the right operand (the "rvalue") to the left one (the "lvalue") π
The former contents of the left operand become the most significant part of the resulting bit vector, and the right operand becomes the least significant part.
Since bit vectors are stored in "least order bit first" order, this actually requires the left operand to be shifted "up" by the length of the right operand, which is then copied to the now freed least significant part of the left operand π
If the right operand is a Perl scalar, it's first converted to a bit vector of the same size as the left operand β but only provided that the configuration states that scalars are to be regarded as indices, decimal strings or enumerations.
If the configuration says scalars are to be regarded as hexadecimal or boolean strings, though, then those strings get converted to bit vectors matching the length of the input string β i.e. four times the length for hexadecimal strings (because each hexadecimal digit is worth 4 bits) and once the length for binary strings. iykyk π
$vector x= $factor;β this statement replaces the given bit vector with a concatenation of as many copies of the original contents as the factor (the right operand) specifies π¦
If the factor is zero, the given bit vector is resized to a length of zero bits.
If the factor is one, the given bit vector is not changed at all. Iconic, honestly.
$vector <<= $bits;β this statement moves the contents of the given bit vector left by$bitspositions (towards the most significant bit).
Meaning: the $bits most significant bits are lost, all the other bits move up by $bits positions, and the $bits least significant bits left unoccupied by the shift are all set to zero β¨
If $bits is greater than the number of bits of the given bit vector, the bit vector is erased completely (i.e. all bits cleared). Rip π
$vector >>= $bits;β this statement moves the contents of the given bit vector right by$bitspositions (towards the least significant bit).
Meaning: the $bits least significant bits are lost, all the other bits move down by $bits positions, and the $bits most significant bits left unoccupied by the shift are all set to zero π
If $bits is greater than the number of bits of the given bit vector, the bit vector is erased completely (i.e. all bits cleared). Same tragic ending π₯
$vector1 |= $vector2;β performs a bitwise OR between the two operands and stores the result in the left operand.
This is the same as calculating the union of two sets π
$vector1 &= $vector2;β performs a bitwise AND between the two operands and stores the result in the left operand.
This is the same as calculating the intersection of two sets π
$vector1 ^= $vector2;β performs a bitwise XOR (exclusive-or) between the two operands and stores the result in the left operand.
This is the same as calculating the symmetric difference of two sets β¨
$vector1 += $vector2;β configuration time again (see the methodConfiguration()description for more). Either it performs a bitwise OR between the two operands (i.e. the union of two sets) β the default behaviour β or it calculates the sum of the two numbers stored in the two operands.
Either way, the result is stored in the left operand π«Ά
$vector1 -= $vector2;β configuration check once more (methodConfiguration(), bestie). Either it calculates the set difference of the two sets represented in the two operands β the default behaviour β or it calculates the difference of the two numbers stored in the two operands.
Result goes in the left operand πΈ
$vector1 *= $vector2;β yep, configuration again (seeConfiguration()). Either it performs a bitwise AND between the two operands (i.e. the intersection of two sets) β the default behaviour β or it calculates the product of the two numbers stored in the two operands.
Result lands in the left operand π
$vector1 /= $vector2;β this statement puts the result of the division of the two numbers stored in the two operands into the left operand π$vector1 %= $vector2;β this statement puts the remainder of the division of the two numbers stored in the two operands into the left operand β¨$vector1 **= $vector2;β this statement puts the result of the exponentiation of the left operand elevated to the right operand's power into the left operand π++$vector,$vector++β this operator performs pre- and post-incrementation of the given bit vector.
The value returned by this term is a reference of the given bit vector object (after or before the incrementation, respectively) π¦
--$vector,$vector--β this operator performs pre- and post-decrementation of the given bit vector.
The value returned by this term is a reference of the given bit vector object (after or before the decrementation, respectively) π
($vector1 cmp $vector2)β returns-1if$vector1is less than$vector2,0if$vector1and$vector2are the same, and1if$vector1is greater than$vector2.
Heads up: this comparison assumes UNSIGNED bit vectors β
($vector1 eq $vector2)β returns true (1) if the contents of the two operands are the same, and false (0) otherwise π($vector1 ne $vector2)β returns true (1) if the two operands differ, and false (0) otherwise π($vector1 lt $vector2)β returns true (1) if$vector1is less than$vector2, and false (0) otherwise.
This comparison assumes UNSIGNED bit vectors β
($vector1 le $vector2)β returns true (1) if$vector1is less than or equal to$vector2, and false (0) otherwise.
Also assumes UNSIGNED bit vectors π
($vector1 gt $vector2)β returns true (1) if$vector1is greater than$vector2, and false (0) otherwise.
Again: UNSIGNED bit vectors assumed πΈ
($vector1 ge $vector2)β returns true (1) if$vector1is greater than or equal to$vector2, and false (0) otherwise.
Yup, UNSIGNED assumed too π
($vector1 <=> $vector2)β returns-1if$vector1is less than$vector2,0if$vector1and$vector2are the same, and1if$vector1is greater than$vector2.
Plot twist: this comparison assumes SIGNED bit vectors π
($vector1 == $vector2)β returns true (1) if the contents of the two operands are the same, and false (0) otherwise β¨($vector1 != $vector2)β returns true (1) if the two operands differ, and false (0) otherwise π₯($vector1 < $vector2)β configuration moment (see the methodConfiguration()for the full deets). Either it returns true (1) if$vector1is a true subset of$vector2(and false (0) otherwise) β that's the default behaviour β or it returns true (1) if$vector1is less than$vector2(and false (0) otherwise).
That second comparison assumes SIGNED bit vectors π§
($vector1 <= $vector2)($vector1 <= $vector2)β okay so depending on your config (theConfiguration()method spill the tea on all that, go read it π ), this one either gives you back true (1) when$vector1is a subset of$vector2β false (0) otherwise β because that's the default behaviour, bestie. Or, if you've flipped the config, it returns true (1) when$vector1is less than or equal to$vector2(false (0) otherwise).
That second flavour of comparison assumes SIGNED bit vectors. No cap.
($vector1 > $vector2)β again, config-dependent (yes,Configuration(), same drill as above π«Ά). Default behaviour: true (1) if$vector1is a true superset of$vector2, false (0) otherwise. Configured the other way: true (1) if$vector1is greater than$vector2, false (0) otherwise.
The latter comparison assumes SIGNED bit vectors.
($vector1 >= $vector2)β config check one more time (seeConfiguration(), you know the vibe β¨). Either it returns true (1) if$vector1is a superset of$vector2(false (0) otherwise) β that's the default, periodt β or it returns true (1) if$vector1is greater than or equal to$vector2(false (0) otherwise).
The latter comparison assumes SIGNED bit vectors.
π SEE ALSO π
Go touch some other docs, iykyk:
β VERSION β
This man page documents Bit::Vector::Overload version 7.4. Slay.
π AUTHOR π
The mastermind behind all this:
Steffen Beyer
mailto:STBEY@cpan.org
http://www.engelschall.com/u/sb/download/
π COPYRIGHT π
Copyright (c) 2000 - 2013 by Steffen Beyer. All rights reserved.
π LICENSE π
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!
π DISCLAIMER π
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. (Yes, that's the scary all-caps part. Respect it. π)
See the "GNU General Public License" for more details.
And that's the whole page, bestie β you made it to the end, main character energy fr fr π¦ This rewrite was serving Bit::Vector::Overload version 7.4 realness, brought to you by the perl v5.42.0 docs, dated 2025-02-06. Go forth and overload responsibly π
πΈ
π¦ where this page comes from
- opensuse-leap 16.0: perl-Bit-Vector 7.400.0-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Bit-Vector 7.400.0-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Bit-Vector 7.400.0-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Bit-Vector 7.400.0-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms