Questions about this topic? Sign up to ask in the talk tab.

Perl/Basics/Boolean Logic/Bitwise Manipulations

From NetSec
Jump to: navigation, search

Perl's bitwise manipulations cover the syntax for performing bitwise math on variables.

AND

  • & - The AND operator.
 
my $num = 10;
$num = $num & 25;
print $num . "\n";
 

NOT

  • ~ - The NOT operator
 
my $num = 10;
$num = ~$num;
print $num . "\n";
 

OR

  • | - The OR operator
 
my $num = 10;
$num = $num | 25;
print $num . "\n";
 

XOR

  • ^ - The xor (exclusive or) operator
 
my $num = 10;
$num = $num ^ 25;
print $num . "\n";
 

Bit Shifting

  • << - The shift left operator
  • >> - The shift right operator

<syntaxhighlight lang="perl">my $num = 10; $num = $num << 2; #Shift left two bits $num = $num >> 2; #Shift right two bits print $num . "\n"; </syntaxhighlight>

Bit Rotation

Perl bit rotation requires the Bit::ShiftReg package from CPAN. More information available there.