Bitwise operators
Bitwise operators are useful dealing with bit masks, but in normal practice, they are not so easy to use, and so you will not encounter them very often. However, since they available in Bash we are going to have a look at them with some examples.
Left shift (<<)
The bitwise left shift operators simply multiplies by 2 a value for each shift position; the following example will make everything more clear:
zarrelli:~$ x=10 ; echo $((x<<1)) 20 zarrelli:~$ x=10 ; echo $((x<<2)) 40 zarrelli:~$ x=10 ; echo $((x<<3)) 80 zarrelli:~$ x=10 ; echo $((x<<4)) 160
What happened? As we said before, the bitwise work on a bit mask, so let's start converting the integer 10
to its binary representation in 16-bit and using a power of 2 table to check the values.
In this case, a simple method to represent a decimal in a binary form is to use the power of two notations, starting with dividing our integer in a sum of power of two numbers. In our example, the highest...