# Perl Bitwise Operations

## Perl Bitwise Operators

Foreword: In this tutorial I talk about the bitwise NOT operator, bitwise AND operator, bitwise OR operator and bitwise Exclusixe OR operator; I also talk about bitwise shift operators.

By: Chrysanthus Date Published: 26 Jul 2016

### Introduction

In this tutorial I talk about the bitwise NOT operator, bitwise AND operator, bitwise OR operator and bitwise Exclusixe OR operator; I also talk about bitwise shift operators. Bitwise operators operate on bits.

Bitwise Logical Operators

Bitwise NOT Operator
The bitwise NOT operator is ~. The bitwise NOT of the byte,

01001101

is

10110010

Remember, 1 means true and 0 means false. The following code does this with the computer:

use strict;

my \$byte8 = pack('B8', '01001101');

my \$answer_byte = ~\$byte8;

The input and output are byte strings.

Bitwise AND
The bitwise AND operator is &. The following is a manual bitwise AND operation:

00110110
01010100
--------
00010100
========

The following code does this with the computer:

use strict;

my \$operandL = pack('B8', '00110110');
my \$operandR = pack('B8', '01010100');

my \$answer_byte = \$operandL & \$operandR;

Bitwise OR
The bitwise OR operator is |. The following is a manual bitwise OR operation:

10111010
00101001
--------
10111011
========

The following code does this with the computer:

use strict;

my \$operandL = pack('B8', '10111010');
my \$operandR = pack('B8', '00101001');

my \$answer_byte = \$operandL | \$operandR;

Bitwise XOR
Bitwise exclusive OR is OR where true OR true is false (the other OR actions remaining the same). The bitwise XOR operator is ^. The following is a manual bitwise XOR operation:

10111010
00101001
--------
10010011
========

The following code does this with the computer:

use strict;

my \$operandL = pack('B8', '10111010');
my \$operandR = pack('B8', '00101001');

my \$answer_byte = \$operandL ^ \$operandR;

Bitwise Shift Operators

Left Shift Operator
The left shift operator is, <<. It shifts all the bits to the left, a number of places, adding corresponding number of zeroes to the right. It operates on integers (whole numbers). So you have to do some conversion. The syntax is:

operand << n

where n is the number of places to be shifted. The left shift of,

11101011

by 2 places is

10101100

discarding the extra result bits on the left. The following code does the above 2 place shifting, in the computer:

use strict;

my \$operand_byte = pack('B8', '11101011');

my \$operand_int = unpack('C', \$operand_byte);

my \$answer_int = \$operand_int << 2;

Right Shift Operator
The right shift operator is, >>. It shifts all the bits to the right, a number of places, adding corresponding number of zeroes to the left. It operates on integers (whole numbers). So you have to do some conversion.  The syntax is:

operand >> n

where n is the number of places to be shifted. The right shift of,

11101011

by 2 places is

00111010

with the extra result bits on the right discarded.

The following code does the above 2 place shifting, in the computer:

use strict;

my \$operand_byte = pack('B8', '11101011');

my \$operand_int = unpack('C', \$operand_byte);

my \$answer_int = \$operand_int >> 2;

That is it for this tutorial.

Chrys

Internet Sockets and Perl
Perl pack and unpack Functions
Writing MySQL Protocol Packets in PurePerl
Developing a PurePerl MySQL API
Using the PurePerl MySQL API