13.2 Binary Arithmetic
13.2 Binary Arithmetic
We can perform bit-wise operations on integers. The operators available are &, | and ^ for bit-wise AND, OR, and XOR, respectively. The following simple program illustrates these operations.
Program 13.1
#!/usr/bin/perl
#binary .pl
use strict;
my @CONVERSIONS = qw(000 001 010 011 100 101 110 111);
my $a = 0b100111;
print "a = ", conv2bin($a), "\n";
my $b = 074;
print "b = ", conv2bin($b), "\n";
#This is the only bit-wise and operator. && and "and" are not bit-wise and
my $aAndb = $a & $b;
print "a & b = ", conv2bin($aAndb), "\n";
#This is the only bit-wise or operator. || and "or" are not bit-wise or
my $aOrb = $a | $b;
print "a | b = ", conv2bin($aOrb), "\n";
#Bit-wise XOR operation
my $aXorb = $a ^ $b;
print "a xor b = ", conv2bin($aXorb), "\n";
#convert a decimal integer to a binary printable string
sub conv2bin{
my $octal = sprintf ("%o", $_[0]);
my @threeBitSeqs = map {$CONVERSIONS [$_]} (split //, $octal);
return (join "", @threeBitSeqs);
}
The program assigns numeric integer values to two scalars. $a and $b. Integer values can be assigned to a variable in several ways. We see see two such possibilities in the program. $a is assigned the binary value 0b100111. The 0b in the front indicates that it is a binary value. $b is assigned the octal or base 8 value of 070. The zero in the front suggests it is octal. Integer values can be assigned as regular decimal or hexa-decimal as well. Thus, the variable $a can be
assigned the same value in the following four ways, each having the same effect.
my $a = 0b100111;
my $a = 047;
my $a = 39;
my $a = 0x27;
The last two assignments specify the value in decimal and hexa-decimal (base 16), respectively.
Because we want to illustrate the binary operations of AND, OR and XOR, we want to print the numbers in binary format. This requires writing the a small subroutine since the function sprintf (or, printf) does not give us a direct way to do so. It should be noted that sprintf prints a string following a format specification. sprintf allows one to print an integer in octal or hexa-decimal, but unfortunately, not in binary base. The subroutine we write first converts the input number into a string of octal digits. Next, the octal digits are split individually, and each octal digit converted to the appropriate 3-bit string sequence by a simple lookup of the global table @CONVERSIONS. Finally, the subroutine concatenates the 3-bit sequences and returns the binary string.
Perl’s &, |, and ^ perform bit-by-bit AND, OR, and XOR operations, respectively, Perl’s && and and do not perform bit-wise AND operation. They are logical operations returning true or false. The difference between the two is that and has low precedence so that in complex logical operations, it is not necessary to use many parenthesis pairs. Both are short-circuit operators. In other words, if several values are ANDed, && or and continues going from
left to right only if there is a chance of success. If it encounters a value that is deemed false, it stops. && returns the last value evaluated. Similarly, || and or are not bit-wise operations, returning true or false. || returns the last value evaluated. It is short-circuit operation and returns true when it first encounter a non-false value. or is also a short-circuit operation.
