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

Perl/Basics/Boolean Logic/Bitwise Manipulations

From NetSec
Revision as of 02:31, 19 July 2012 by Chantal21I (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.