I have this line:
if [ $((M & ;0x10000000)) -eq 0 ]; then
But I don't know what is the meaning of this line. M
is the variable which contains a number. But I don't know what & ; 0x10000000
will do with is number.
Let's analyse your line step by step and look at man bash
for answers:
$(( expression ))
:
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is
$((expression))
. The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the expression undergo parameter expansion, string expansion, command substitution, and quote removal. Arithmetic expansions may be nested. The evaluation is performed according to the rules listed below under ARITHMETIC EVALUATION. Ifexpression
is invalid, bash prints a message indicating failure and no substitution occurs.
This tells us that we M & 0x10000000
will be evaluated arithmetically.
M & 0x10000000
:
The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the
let
and declare builtin commands andArithmetic Expansion
). Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence, associativity, and values are the same as in the C language. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.
&
bitwise AND
This tell us that your variable M
will undergo a bitwise AND
operation with the hexadecimal value 0x10000000
. The latter is the hexadecimal representation of 268435456 or 2^28. So in essence,the operation M & 0x10000000
will return 0x10000000
if the 29th bit of M
is set to 1
otherwise it will return 0x0
(note it is one bit more as 2^0 = 1).
[ EXPRESSION ]
: execution of the binary /bin/test
Exit with the status determined by EXPRESSION.
INTEGER1 -eq INTEGER2
INTEGER1
is equal toINTEGER2
This tells us that the expression [ $((M & 0x10000000)) -eq 0 ]
will return a true
if the result of M & 0x10000000
equals ZERO, otherwise it will return false.
So in short,
if [ $((M & 0x10000000)) -eq 0 ]; then
will perform a test on M
to check if the 28th bit of M
is set. If it is not set it will perform the action defined in the if
statement.
An alternative notation would have been
if (( M & 0x10000000 == 0x0 )); then
That is checking if the 29th bit of the number in M
is clear.
$ M=0xEFFFFFFF; [ $((M & 0x10000000)) -eq 0 ] && echo CLEAR || echo SET
CLEAR
$ M=0xFFFFFFFF; [ $((M & 0x10000000)) -eq 0 ] && echo CLEAR || echo SET
SET
$ M=0x10000000; [ $((M & 0x10000000)) -eq 0 ] && echo CLEAR || echo SET
SET
$ M=0; [ $((M & 0x10000000)) -eq 0 ] && echo CLEAR || echo SET
CLEAR
The &
is the bitwise and operator, it returns a number with the bits sets where the operands have both the same bits set. See https://en.wikipedia.org/wiki/Bitwise_operation
More info about bitmasks: https://en.wikipedia.org/wiki/Mask_(computing)
https://www.tldp.org/LDP/abs/html/dblparens.html
The $((expression))
will do arithmetic expansion.
The line if [ $((M & 0x10000000)) -eq 0 ]; then
will check if the 29th bit of $M
variable is zero. If this is the case it will execute what follows then
statement.
User contributions licensed under CC BY-SA 3.0