Pcore::Util::Bit - bitwise operations with mask
Pcore::Util::Bit::set_bit($binary, $mask[, $bits]); Pcore::Util::Bit::clear_bit($binary, $mask[, $bits]); Pcore::Util::Bit::inverse_bit($binary, $mask); Pcore::Util::Bit::set_bit(0b1111_0000, 0b0011_1000); # returns 0b1111_1000 Pcore::Util::Bit::set_bit(0b1111_0000, 0b0011_1000, 0b101); # returns 0b1110_1000 Pcore::Util::Bit::clear_bit(0b1111_0000, 0b0011_1000); # returns 0b1100_0000 Pcore::Util::Bit::clear_bit(0b1111_0000, 0b0011_1000, 0b101); # returns 0b1101_0000 Pcore::Util::Bit::inverse_bit(0b1111_0000, 0b0011_1000); # returns 0b1100_1000
$mask shows which bits in $binary we would like to modify. Other bits will stay untouched.
In two-params call all corresponding bits in $binary value will be setted or cleared correspondently.
In three-params set_bit() call all corresponding bits in $binary value will be setted as it defined in $bits value.
In three-params clear_bit() call all corresponding bits in $binary value will be setted as it defined in inversed $bits value.
76543210 0b10101010 - $binary 0b00110011 - $mask 0b00110000 - $bits ----------- set_bit($binary, $mask, $bits) - bits[5, 4] - will be setted, bits[1, 0] - will be cleared clear_bit($binary, $mask, $bits) - bits[5, 4] - will be cleared, bits[1, 0] - will be setted
Result returned if expected otherwise $binary value will be modified in-place.
Accept only two parameters: $binary and $mask.
Inverse corresponding bits in $binary value according to $mask.
NOTE: this operation is simple XOR.
Маска определяет, какие биты будем модифицировать в исходном скаляре. Остальные биты не будут затронуты ни при каких условиях.
set_bit(), clear_bit() с ДВУМЯ параметрами устанавливает или обнуляет ВСЕ биты, попадающие под маску, соответственно.
Дополнительный ТРЕТИЙ параметр $bits позволяет указать как именно модифицировать биты, попадающие под маску:
set_bit() - устанавливает биты под маской в те значения, которые соответствуют им в $bits соответственно. Т.е. 1 биты в $bits будут 1 в $binary, 0 в $bits будут 0 в $binary. Вызов set_bit($a, $b) эквивалентен вызову set_bit($a, $b, $b), так как в обоих случаях устанавливаются все биты под маской;
clear_bit() - то-же, что и clear_bit с инвертированным $bits параметром;
Bitwise operators limited to a certain number of bits (32 bits of 64 bits, depending on your architecture). If we go over this, we'll get an error.
To install Pcore, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Pcore
CPAN shell
perl -MCPAN -e shell install Pcore
For more information on module installation, please visit the detailed CPAN module installation guide.