This tutorial explains bitwise operators in Perl with code examples.

What is Bitwise Operators

Bitwise operators are also called binary operands. Binary operators are operators that work with two operands. These operators convert operands into bits and do bitwise comparisons. Perl has the following bitwise operator.

• Bitwise AND or Logical Inclusive AND(&) if both operand values are 1, then return 1, else 0.

• Bitwise OR or Logical Inclusive OR (|) if one or both operand is 1, return 1, else 0.

• Bitwise XOR or Logical Exclusive OR(^) if one of the bits is 1, return 1. if both are 0 or 1, return 0.

Perl Bitwise Operators

Bitwise operators are used for operations on bits of given operands. Operands can be numbers or strings.

Syntax:

Operand1 operator operand1

if one or both operands are numbers, First convert to integer, do bitwise of integers. If operands are strings, It does a bitwise comparison of strings.

For example, We have operand1(op1)=6 and operand2(op2)=1 values. Operand1=6(0110) Operand1=1(0001)

OperationSymbolDescriptionResult
AND&Bitwise AND of two operands\$op1 & \$op2 is 0
OR``Bitwise OR of two operands
XOR^Bitwise XOR of two operands\$op1 ^ \$op2 is 7

Perl Bitwise Operator Example

Here is the Bitwise operator Number Example

# Bitwise number example
my \$op1= 4;
my \$op2 = 2;

my \$result= \$op1 & \$op2;
print "Bitwise AND of \$op1 and \$op2 is \$result \n";

\$result= \$op1 | \$op2;
print "Bitwise OR of \$op1 and \$op2 is \$result \n";

\$result= \$op1 ^ \$op2;
print "Bitwise XOR of \$op1 and \$op2 is \$result \n";

Output:

Bitwise AND of 4 and 2 is 0
Bitwise OR of 4 and 2 is 6
Bitwise XOR of 4 and 2 is 6

Here is the Bitwise operator String example

# Bitwise string example
my \$op1= "str4";
my \$op2 = "str2";

\$result= \$op1 & \$op2;
print "Bitwise AND of \$op1 and \$op2 is \$result \n";

\$result= \$op1 | \$op2;
print "Bitwise OR of \$op1 and \$op2 is \$result \n";

\$result= \$op1 ^ \$op2;
print "Bitwise XOR of \$op1 and \$op2 is \$result \n";

Output:

Bitwise AND of str4 and str2 is str0
Bitwise OR of str4 and str2 is str6
Bitwise XOR of str4 and str2 is 

What is the bitwise AND operator in Perl?

Bitwise AND operators work on two operands, operands can be string or numeric. First, It converts the given operand to bits and does a comparison of each bit. It returns 1, if both bits are 1, else returns zero.

What is the bitwise OR operator in Perl?

Bitwise OR operators work on two operands, operands can be string or numeric. First, It converts the given operand to bits and does a comparison of each bit. It returns 1, if On or both bits are 1, else returns zero.

What is the bitwise XOR operator in Perl?

Bitwise XOR operators work on two operands, operands can be string or numeric. First, It converts the given operand to bits and does a comparison of each bit. if one of the bits is 1, return 1, and if both are 0 or 1, return 0.

How many bitwise operators are there?

There are three Bitwise operators in Perl, It contains Bitwise And, OR, and XOR that work on two operands.