- "$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 (!).
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"" will cause scalar
input to be considered as being in hexadecimal, i.e.,
""$vector ^= 5;"" will flip
bit #0 and bit #2 (because hexadecimal
"5" is binary
"0101").
(Note though that hexadecimal input should always be enclosed
in quotes, otherwise it will be interpreted as a decimal number by Perl!
The example relies on the fact that hexadecimal
"0-9" and decimal
"0-9" are the same.)
The keyword
""^bin"" will cause scalar
input to be considered as being in binary format. All characters except
"0" and
"1" are forbidden in this case (i.e.,
produce a syntax error).
""$vector ^=
'0101';"", for instance, will flip bit #0 and bit
#2.
The keyword
""^dec"" causes scalar input
to be considered as integers in decimal format, i.e.,
""$vector ^= 5;"" will flip
bit #0 and bit #2 (because decimal "5"
is binary "0101").
(Note though that all decimal input should be enclosed in
quotes, because for large numbers, Perl will use scientific notation
internally for representing them, which produces a syntax error because
scientific notation is neither supported by this module nor needed.)
Finally, the keyword
""^enum"" causes scalar
input to be considered as being a list ("enumeration") of
indices and ranges of (contiguous) indices, i.e.,
""$vector |=
'2,3,5,7-13,17-23';"" will cause bits #2, #3, #5, #7
through #13 and #17 through #23 to be set.
OPERATOR SEMANTICS:
Several overloaded operators can have two 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 operators are NOT affected by the
"operator semantics" setting).
STRING OUTPUT:
There are four methods which 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 this
method does not return a human-readable string).
(For conversion to octal, see the description of the method
""Chunk_List_Read()"".)
Therefore, there are four possible formats into which a bit
vector can be converted when it is enclosed in double quotes, for
example:
print "\$vector = '$vector'\n";
$string = "$vector";
Hence you can set "string output" to four different
values: To "hex" for hexadecimal format (which is the
default), to "bin" for binary format, to "dec" for
conversion to decimal numbers and to "enum" for conversion to
enumerations (".newsrc" style sets).
BEWARE that the conversion to decimal numbers is
inherently slow; it can easily take up several seconds for a single
large bit vector!
Therefore you should store the decimal strings returned to you
rather than converting a given bit vector again.
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 that you can enter the same given 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 a
"Bit::Vector" object 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 methods
""to_Hex()"",
""to_Bin()"",
""to_Dec()"" and
""to_Enum()""), it is of
course desirable to be able to choose which of these methods should be
applied in this case.
This can actually be done by changing the configuration of
this module using the method
""Configure()"" (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 a "Bit::Vector" object 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 is possible to use a Perl variable containing the
reference of a "Bit::Vector" object 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 method
""is_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
method ""Configuration()""
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 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!
- "$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 "$bits"
most significant bits are lost, all other bits move up by
"$bits" positions, and the
"$bits" least significant bits that
have been left unoccupied by this shift are all set to zero.
If "$bits" is 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 "$bits"
least significant bits are lost, all other bits move down by
"$bits" positions, and the
"$bits" most significant bits that
have been left unoccupied by this shift are all set to zero.
If "$bits" is 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.
This is the same as calculating the union of two sets.
- "$vector1 & $vector2"
This term returns a new bit vector object which is the result
of a bitwise AND operation between the two bit vector operands.
This is the same as calculating the intersection of two
sets.
- "$vector1 ^ $vector2"
This term returns a new bit vector object which is the result
of a bitwise XOR (exclusive-or) operation between the two bit vector
operands.
This is the same as calculating the symmetric difference of
two sets.
- "$vector1 + $vector2"
Depending on the configuration (see the description of the
method ""Configuration()""
for more details), this term either returns a new bit vector object
which is the result of a bitwise OR operation between the two bit vector
operands (this is the same as calculating the union of two sets) - which
is the default behaviour, or it returns a new bit vector object which
contains the sum of the two numbers stored in the two bit vector
operands.
- "$vector1 - $vector2"
Depending on the configuration (see the description of the
method ""Configuration()""
for more details), this term either returns a new bit vector object
which is the set difference of the two sets represented in the two bit
vector operands - which is the default behaviour, or it returns a new
bit vector object which contains the difference of the two numbers
stored in the two bit vector operands.
- "$vector1 * $vector2"
Depending on the configuration (see the description of the
method ""Configuration()""
for more details), this term either returns a new bit vector object
which is the result of a bitwise AND operation between the two bit
vector operands (this is the same as calculating the intersection of two
sets) - which is the default behaviour, or it returns a new bit vector
object which contains the product of the two numbers stored in the two
bit vector operands.
- "$vector1 / $vector2"
This term returns a new bit vector object containing the
result of the division of the two numbers stored in the two bit vector
operands.
- "$vector1 % $vector2"
This term returns a new bit vector object containing the
remainder of the division of the two numbers stored in the two bit
vector operands.
- "$vector1 ** $vector2"
This term returns a new bit vector object containing the
result of the exponentiation of the left bit vector elevated to the
right bit vector's power.
- "$vector1 .= $vector2;"
This statement "appends" the right bit vector
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 is first converted
to a bit vector of the same size as the left 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.
- "$vector x= $factor;"
This statement replaces the given bit vector by a
concatenation of as many copies of the original contents of the given
bit vector 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.
- "$vector <<= $bits;"
This statement moves the contents of the given bit vector left
by "$bits" positions (towards the most
significant bit).
This means that the "$bits"
most significant bits are lost, all other bits move up by
"$bits" positions, and the
"$bits" least significant bits that
have been left unoccupied by this shift are all set to zero.
If "$bits" is greater than
the number of bits of the given bit vector, the given bit vector is
erased completely (i.e., all bits are cleared).
- "$vector >>= $bits;"
This statement moves the contents of the given bit vector
right by "$bits" positions (towards
the least significant bit).
This means that the "$bits"
least significant bits are lost, all other bits move down by
"$bits" positions, and the
"$bits" most significant bits that
have been left unoccupied by this shift are all set to zero.
If "$bits" is greater than
the number of bits of the given bit vector, the given bit vector is
erased completely (i.e., all bits are cleared).
- "$vector1 |= $vector2;"
This statement performs a bitwise OR operation between the two
bit vector operands and stores the result in the left operand.
This is the same as calculating the union of two sets.
- "$vector1 &= $vector2;"
This statement performs a bitwise AND operation between the
two bit vector operands and stores the result in the left operand.
This is the same as calculating the intersection of two
sets.
- "$vector1 ^= $vector2;"
This statement performs a bitwise XOR (exclusive-or) operation
between the two bit vector operands and stores the result in the left
operand.
This is the same as calculating the symmetric difference of
two sets.
- "$vector1 += $vector2;"
Depending on the configuration (see the description of the
method ""Configuration()""
for more details), this statement either performs a bitwise OR operation
between the two bit vector operands (this is the same as calculating the
union of two sets) - which is the default behaviour, or it calculates
the sum of the two numbers stored in the two bit vector operands.
The result of this operation is stored in the left
operand.
- "$vector1 -= $vector2;"
Depending on the configuration (see the description of the
method ""Configuration()""
for more details), this statement either calculates the set difference
of the two sets represented in the two bit vector operands - which is
the default behaviour, or it calculates the difference of the two
numbers stored in the two bit vector operands.
The result of this operation is stored in the left
operand.
- "$vector1 *= $vector2;"
Depending on the configuration (see the description of the
method ""Configuration()""
for more details), this statement either performs a bitwise AND
operation between the two bit vector operands (this is the same as
calculating the intersection of two sets) - which is the default
behaviour, or it calculates the product of the two numbers stored in the
two bit vector operands.
The result of this operation is stored in the left
operand.
- "$vector1 /= $vector2;"
This statement puts the result of the division of the two
numbers stored in the two bit vector operands into the left operand.
- "$vector1 %= $vector2;"
This statement puts the remainder of the division of the two
numbers stored in the two bit vector 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)"
This term returns
""-1"" if
"$vector1" is less than
"$vector2",
"0" if
"$vector1" and
"$vector2" are the same, and
"1" if
"$vector1" is greater than
"$vector2".
This comparison assumes UNSIGNED bit vectors.
- "($vector1 eq $vector2)"
This term returns true ("1")
if the contents of the two bit vector operands are the same and false
("0") otherwise.
- "($vector1 ne $vector2)"
This term returns true ("1")
if the two bit vector operands differ and false
("0") otherwise.
- "($vector1 lt $vector2)"
This term returns true ("1")
if "$vector1" is less than
"$vector2", and false
("0") otherwise.
This comparison assumes UNSIGNED bit vectors.
- "($vector1 le $vector2)"
This term returns true ("1")
if "$vector1" is less than or equal to
"$vector2", and false
("0") otherwise.
This comparison assumes UNSIGNED bit vectors.
- "($vector1 gt $vector2)"
This term returns true ("1")
if "$vector1" is greater than
"$vector2", and false
("0") otherwise.
This comparison assumes UNSIGNED bit vectors.
- "($vector1 ge $vector2)"
This term returns true ("1")
if "$vector1" is greater than or equal
to "$vector2", and false
("0") otherwise.
This comparison assumes UNSIGNED bit vectors.
- "($vector1 <=> $vector2)"
This term returns
""-1"" if
"$vector1" is less than
"$vector2",
"0" if
"$vector1" and
"$vector2" are the same, and
"1" if
"$vector1" is greater than
"$vector2".
This comparison assumes SIGNED bit vectors.
- "($vector1 == $vector2)"
This term returns true ("1")
if the contents of the two bit vector operands are the same and false
("0") otherwise.
- "($vector1 != $vector2)"
This term returns true ("1")
if the two bit vector operands differ and false
("0") otherwise.
- "($vector1 < $vector2)"
Depending on the configuration (see the description of the
method ""Configuration()""
for more details), this term either returns true
("1") if
"$vector1" is a true subset of
"$vector2" (and false
("0") otherwise) - which is the
default behaviour, or it returns true
("1") if
"$vector1" is less than
"$vector2" (and false
("0") otherwise).
The latter comparison assumes SIGNED bit vectors.
- "($vector1 <= $vector2)"
Depending on the configuration (see the description of the
method ""Configuration()""
for more details), this term either returns true
("1") if
"$vector1" is a subset of
"$vector2" (and false
("0") otherwise) - which is the
default behaviour, or it returns true
("1") if
"$vector1" is less than or equal to
"$vector2" (and false
("0") otherwise).
The latter comparison assumes SIGNED bit vectors.
- "($vector1 > $vector2)"
Depending on the configuration (see the description of the
method ""Configuration()""
for more details), this term either returns true
("1") if
"$vector1" is a true superset of
"$vector2" (and false
("0") otherwise) - which is the
default behaviour, or it returns true
("1") if
"$vector1" is greater than
"$vector2" (and false
("0") otherwise).
The latter comparison assumes SIGNED bit vectors.
- "($vector1 >= $vector2)"
Depending on the configuration (see the description of the
method ""Configuration()""
for more details), this term either returns true
("1") if
"$vector1" is a superset of
"$vector2" (and false
("0") otherwise) - which is the
default behaviour, or it returns true
("1") if
"$vector1" is greater than or equal to
"$vector2" (and false
("0") otherwise).
The latter comparison assumes SIGNED bit vectors.