Bitwise Operators: 

A bitwise operator is an operator which used to perform bitwise operations on bit patterns. It converts the operands into binary data and then perform necessary operations bit by bit. Bitwise operators are of 6 types,

Bitwise AND ( & ) :

A Bitwise AND operator performs binary AND operation bit by bit on the operands.

It converts the given input to binary digit and then perform AND operation bit by bit. Finally it returns the decimal value.

Example:

Consider two values 54 and 18.

The binary value for the given values are,

54 -------------> 00110110

18 -------------> 00010010
---------------
00010010

The decimal value for 00010010 is 18.

To perform such operation we use Bitwise AND operator in shell script.

Open a file as band.sh

$ vim band.sh

Paste the below code

Code:

#!/bin/sh

#Getting inputs
read -p 'Enter val1 : ' val1
read -p 'Enter val2 : ' val2

BAND=$(( val1 & val2 ))
echo Bitwise AND of $val1 and $val2 is $BAND

Run the file,

jhony@ljlinux:~$ ./band.sh
Enter val1 : 54
Enter val2 : 18
Bitwise AND of 54 and 18 is 18

Bitwise OR ( | ) :

Bitwise | operator performs binary OR operation bit by bit on the operands.

It converts the given input to binary digit and then perform OR operation bit by bit. Then it returns the decimal value of the result.

Example:

Considering two values 36 and 72.

The binary value for the given values are,

36 -------------> 00100100

72 -------------> 01001000
---------------
01101100

The decimal value for 01101100 is 108.

To perform such operation we use Bitwise OR operator in shell script.

Open a file as bor.sh

$ vim bor.sh

Paste the below code

Code:

#!/bin/sh

#Getting inputs
read -p 'Enter val1 : ' val1
read -p 'Enter val2 : ' val2

BOR=$(( $val1 | $val2 ))
echo Bitwise OR of $val1 and $val2 is $BOR

Run the file,

jhony@ljlinux:~$ ./bor.sh
Enter val1 : 36
Enter val2 : 72
Bitwise OR of 36 and 72 is 108

Bitwise XOR ( ^ ) : 

Bitwise ^ operator performs binary XOR operation bit by bit on the operands.

It converts the given input to binary digit and then perform XOR operation bit by bit. It compares both operands and returns 1 if both are different and it returns 0 if both the operands are same. Finally it returns the decimal value of the result.

Example:

Considering two values 42 and 27.

The binary value for the given values are,

42 -------------> 00101010

27 -------------> 00110001
---------------
00110001

The decimal value for 00110001 is 49.

For performing such operation we can use Bitwise XOR operator ^ in shell script.

Open a file as bxor.sh

$ vim bxor.sh

Paste the below code

Code:

#!/bin/sh

#Getting inputs
read -p 'Enter val1 : ' val1
read -p 'Enter val2 : ' val2

BXOR=$(( $val1 ^ $val2 ))
echo Bitwise XOR of $val1 and $val2 is $BXOR

Run the file,

jhony@ljlinux:~$ ./bxor.sh
Enter val1 : 42
Enter val2 : 27
Bitwise OR of 42 and 27 is 49

Bitwise compliment ( ~ ) : 

Bitwise ~ operator performs binary NOT operation bit by bit on the operand.

It converts the given input to binary digit and then perform XOR operation bit by bit. It operator is used to invert all of the bits of the operand and returns the decimal value of the result.

Remember it inverts 32 bits of an operand.

Example:

Let us find the Bitwise compliment of 36.

The binary value for 36 is,

36 - 0000000000000000000000100100

~36 - 1111111111111111111111011011

The decimal value for 1111111111111111111111011011 is -36.

For this we can use Bitwise compliment operator ~ in shell script.

Open a file as bcom.sh

$ vim bcom.sh

Paste the below code

Code:

#!/bin/sh

#Getting input
read -p 'Enter the value : ' val

BCOM=$(( ~$val ))
echo Bitwise compliment of $val is $BCOM

Run the file,

jhony@ljlinux:~$ ./bcom.sh
Enter the value : 36
Bitwise compliment of 36 is -37

Right Shift ( >> ) : 

The Right Shift operator shifts the bits of the left operand to right by number of times specified.

It moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0.

For better understanding the Right shift operator always decrease the value of the operand.

Example:

Let us shift the values one, two and three times to the right.

Considering a value 45.

The binary value of 45 is,

45 ---------> 00101101

Now performing Right shift,

45 >> 1 -----> 00010110 = 22
45 >> 2 -----> 00001011 = 11
45 >> 3 -----> 00000101 = 5

Open a file as brs.sh

$ vim brs.sh

Paste the below code

Code:

#!/bin/sh

#Getting input
read -p 'Enter the value : ' val

BRS1=$(( $val >> 1 ))
BRS2=$(( $val >> 2 ))
BRS3=$(( $val >> 3 ))

echo Once right shift of $val is $BRS1
echo Twice right shift of $val is $BRS2
echo Thrice right shift of $val is $BRS3

Run the file,

jhony@ljlinux:~$ ./brs.sh
Enter the value : 45
Once right shift of 45 is 22
Twice right shift of 45 is 11
Thrice right shift of 45 is 5

Left Shift ( << ) : 

The Left Shift operator shifts the bits of the left operand to left by number of times specified.

It moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0.

The Left shift operator will always increase the value of the operand.

Example:

Let us shift the values one, two and three times to the left.

Considering same 45.

And the binary value of 45 is 00101101 as we already know,

45 ---------> 00101101

Now performing Right shift,

45 << 1 -----> 01011010 = 90
45 << 2 -----> 10110100 = 180
45 << 3 -----> 01101000 = 360

Open a file as bls.sh

$ vim bls.sh

Paste the below code

Code:

#!/bin/sh

#Getting input
read -p 'Enter the value : ' val

BLS1=$(( $val << 1 ))
BLS2=$(( $val << 2 ))
BLS3=$(( $val << 3 ))

echo Once left shift of $val is $BLS1
echo Twice left shift of $val is $BLS2
echo Thrice left shift of $val is $BLS3

Run the file,

jhony@ljlinux:~$ ./bls.sh
Enter the value : 45
Once left shift of 45 is 90
Twice left shift of 45 is 180
Thrice left shift of 45 is 360

That's how Bitwise operators works in shell scripts. Feel free to ask if you have any questions.

Comments

  1. Be the first to add a comment.