·  PERL – Syntax – Operators – Bitwise

 

- Info:

  - Bitwise Operators operate on individual bits of integer values: & | ^ ~ << >>

 

- Test.pl

  #Test variables.-------------------------------------------------------------------

  $left       = 0b11011;

  $right      = 0b10010;

 

  #Bitwise operators.----------------------------------------------------------------

  $and        =  $left & $right;    #10010.    1 if both   bits are 1.

  $or         =  $left | $right;    #11011.    1 if either bit  is  1.

  $xor        =  $left ^ $right;    #01001.    1 if        bits are different.

  $invert     = ~$left;             #11111111111111111111111111100100.  Invert bits.

  $shiftLeft  =  $left  << 3;       #11011000. Shift bits to the left  by 3 positions. Fill with 0.

  $shiftRight =  $left  >> 2;       #110.      Shift bits to the right by 2 positions. Fill with 0.

 

  #Display results.------------------------------------------------------------------

  print($and);                      #Replace $and with any other variable from above to see its value.