I'm curious about what exactly the following comparison does, in as much detail as possible, especially relating to the 0x2
and the &
characters and what exactly they do,
if [ $((${nValid} & 0x1)) -eq 1 ]; then
#...snip...
fi
if [ $((${nValid} & 0x2)) -eq 2 ]; then
#...snip...
fi
& is the bitwise AND operator. So you are asking to do a bitwise and between 0x1 and the value that ${nVAlid} is returning.
For more information on bitwise operations look here.
A shell script interprets a number as decimal (base 10), unless that number has a special prefix or notation. A number preceded by a 0 is octal (base 8). A number preceded by 0x is hexadecimal (base 16). A number with an embedded # evaluates as BASE#NUMBER (with range and notational restrictions).
So, in [ $((${nValid} & 0x1)) -eq 1 ]
, $nValid
is anded with 0x1
and compared with decimal 1
. Similarly the second comparison too.
Read this and this for detailed info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With