What is the meaning of $((M &amp ; 0x10000000))?

0

I have this line:

if [ $((M &amp ;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 &amp ; 0x10000000 will do with is number.

bash
shell
asked on Stack Overflow Apr 1, 2019 by Amanda • edited Apr 1, 2019 by Amanda

3 Answers

1

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. If expression 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 and Arithmetic 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 to INTEGER2

    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
answered on Stack Overflow Apr 1, 2019 by kvantour
0

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)

answered on Stack Overflow Apr 1, 2019 by olivecoder • edited Apr 1, 2019 by olivecoder
0

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.

answered on Stack Overflow Apr 1, 2019 by Nikolaos Skalkotos • edited Apr 1, 2019 by Taegyung

User contributions licensed under CC BY-SA 3.0