gpt4 book ai didi

bash - 如何在 if 语句中使用位运算符?

转载 作者:行者123 更新时间:2023-11-29 08:46:40 24 4
gpt4 key购买 nike

我想写这样的东西:

if [[ ( releases["token"] & $MASK ) -eq 1 ]]; then

但我得到的错误是:

unexpected token `&', conditional binary operator expected

如何在 if 语句中使用按位运算符?

最佳答案

逻辑与语法

.... 或:“还有什么要说的?”

乍一看,正如@chepner 的 comment 中指出的那样,问题可能只是语法之一,这会导致编译错误(unexpected token '&', conditional binary operator expected)。事实上,@kev's answer通过使用 arithmetic expansion 来解决这个问题,应用于 @Gustavo's answer 中的 If 语句.

但是,问题如何在 if 语句中使用位运算符 是以更一般的方式表述的,并请求关于如何< em>使用位运算符来检查$MASK(这不是“使用二进制比较时如何避免语法错误”的问题)。

其实可以假设示例代码

if [[ ( releases["token"] & $MASK ) -eq 1 ]]; then

是一项正在寻找解决方案的工作,并被明确标记为“类似...”。现在@kev 的任意假设

 releases["token"]=3
MASK=5

可能掩盖了一个事实,即首先使用 -eq 1 也存在逻辑上的误解。因此,虽然@kev 的答案适用于所选值,但输入的结果例如4 行在

 (((5&4)==1)) && echo YES || echo NO

将打印 NO,即使在这种情况下也期望 YES,因为 45 都设置了第 3 位.

因此,这个答案解决了问题示例中的这个逻辑错误,并尝试对问题的标题进行一般性回答。


要事第一!

... 或:“按位表示的背景”

to understand bitwise comparison it is helpful to visualise numbers in their bit representation (listed top-down in the following example):

|-----------------------------------------------------------|
| | hexadecimal representation of the value |
| bit val | 0 1 2 3 4 5 6 7 8 9 A B C D E F |
|---------|---------------------- bit ----------------------|
| | shows if the given value has the given bit set |
| 0 | - x - x - x - x - x - x - x - x |
| 1 | - - x x - - x x - - x x - - x x |
| 2 | - - - - x x x x - - - - x x x x |
| 3 | - - - - - - - - x x x x x x x x |
|---------|---------------------- val ----------------------|
| shows the value that the given bit adds to the number |
| 0 1 | 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 |
| 1 2 | 0 0 2 2 0 0 2 2 0 0 2 2 0 0 2 2 |
| 2 4 | 0 0 0 0 4 4 4 4 0 0 0 0 4 4 4 4 |
| 3 8 | 0 0 0 0 0 0 0 0 8 8 8 8 8 8 8 8 |
|---------|-------------------------------------------------|
|sum: 15 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| | decimal representation of the value |
|===========================================================|

由以下代码片段创建:

( # start a subshell to encapsulate vars for easy copy-paste into the terminal
# do not use this in a script!
echo ;
echo "|-----------------------------------------------------------|";
echo "| | hexadecimal representation of the value |";
echo "| bit val | 0 1 2 3 4 5 6 7 8 9 A B C D E F |";
echo "|---------|---------------------- bit ----------------------|";
echo "| | shows if the given value has the given bit set |";
mask=0;
for (( bit=0; bit<4; bit++)); do
mask=$((1<<bit));
echo -n "| $bit |"
for ((x=0;x<16;x++)); do ((((x&mask)>0))&&echo -n ' x'||echo -n ' -'); done
echo " |";
done ;
echo "|---------|---------------------- val ----------------------|";
echo "| shows the value that the given bit adds to the number |";
mask=0;
for (( bit=0; bit<4; bit++)); do
mask=$((1<<bit));
echo -n "| $bit $mask |"
for ((x=0;x<16;x++)); do echo -n " $((x&mask))"; done
echo " |";
done ;
echo "|---------|-------------------------------------------------|";
echo "|sum: 15 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |";
echo "| | decimal representation of the value |";
echo "|===========================================================|";
echo ;
)


3 & 54 & 5 的奇怪案例

... or: "==1 vs >0&, |的区别, 和 ^"

if we now check the special case of the examples mentioned, we'll see the shortcoming:

|------------------ value: 3 / mask: 5 -------------------|
| | value | mask | and | or | xor | xnor |
| | 3 | 5 | 3 & 5 | 3 | 5 | 3 ^ 5 | |
| bit val |bit:val|bit:val|bit:val|bit:val|bit:val|bit:val|
|---------|-------|-------|-------|-------|-------|-------|
| 0 1 | 1 1 | 1 1 | 1 1 | 1 1 | 0 0 | 0 0 |
| 1 2 | 2 1 | 0 0 | 0 0 | 1 2 | 1 2 | 1 2 |
| 2 4 | 0 0 | 4 1 | 0 0 | 1 4 | 1 4 | 1 4 |
| 3 8 | 0 0 | 0 0 | 0 0 | 0 0 | 0 0 | 0 0 |
|---------|-------|-------|-------|-------|-------|-------|
|sum: | 3 | 5 | ==> 1 | 7 | 6 | 6 |
|---------|-----------------------------------------------|
|check: | 3 & 5 == 1 ? YES | |
| | 3 & 5 >= 1 ? YES | ==> 3 & 5 > 0 ? YES |
|=========================================================|

|------------------ value: 4 / mask: 5 -------------------|
| | value | mask | and | or | xor | xnor |
| | 4 | 5 | 4 & 5 | 4 | 5 | 4 ^ 5 | |
| bit val |bit:val|bit:val|bit:val|bit:val|bit:val|bit:val|
|---------|-------|-------|-------|-------|-------|-------|
| 0 1 | 0 0 | 1 1 | 0 0 | 1 1 | 1 1 | 1 1 |
| 1 2 | 0 0 | 0 0 | 0 0 | 0 0 | 0 0 | 0 0 |
| 2 4 | 4 1 | 4 1 | 1 4 | 1 4 | 0 0 | 0 0 |
| 3 8 | 0 0 | 0 0 | 0 0 | 0 0 | 0 0 | 0 0 |
|---------|-------|-------|-------|-------|-------|-------|
|sum: | 4 | 5 | ==> 4 | 5 | 1 | 1 |
|---------|-----------------------------------------------|
|check: | 4 & 5 == 1 ? NO | |
| | 4 & 5 >= 1 ? YES | ==> 4 & 5 > 0 ? YES |
|=========================================================|


请特别注意以下检查:

|---------|-----------------------------------------------|
|check: | 3 & 5 == 1 ? YES | |
| | 3 & 5 >= 1 ? YES | ==> 3 & 5 > 0 ? YES |
---------|------------------------------------------------|
|check: | 4 & 5 == 1 ? NO | |
| | 4 & 5 >= 1 ? YES | ==> 4 & 5 > 0 ? YES |
|---------|-----------------------------------------------|


上面的输出是由以下代码片段创建的:

( # start a sub-shell to encapsulate vars for easy copy-paste into the terminal
# do not use this in a script!
echo ;
for o in 3 4; do
echo "|------------------ value: $o / mask: 5 -------------------|";
echo "| | value | mask | and | or | xor | xnor |";
echo "| | $o | 5 | $o & 5 | $o | 5 | $o ^ 5 | |";
echo "| bit val |bit:val|bit:val|bit:val|bit:val|bit:val|bit:val|";
echo "|---------|-------|-------|-------|-------|-------|-------|";
mask=0;
for (( bit=0; bit<4; bit++)); do
mask=$((1<<bit));
echo -n "| $bit $mask "
echo -n " | $(($o&mask)) $((($o&mask)>0))"
echo -n " | $((5&mask)) $(((5&mask)>0))"
echo -n " | $(((($o&mask)&(5&mask))>0)) $((($o&mask)&(5&mask)))"
echo -n " | $(((($o&mask)|(5&mask))>0)) $((($o&mask)|(5&mask)))"
echo -n " | $(((($o&mask)^(5&mask))>0)) $((($o&mask)^(5&mask)))"
echo -n " | $(((($o&mask)^(5&mask))>0)) $((($o&mask)^(5&mask)))"
echo " |";
done ;
echo "|---------|-------|-------|-------|-------|-------|-------|";
echo -n "|sum: | $o | 5 |";
echo " ==> $(($o&5)) | $(($o|5)) | $(($o^5)) | $(($o^5)) |";
echo "|---------|-----------------------------------------------|";
echo -n "|check: | $o & 5 == 1 ? $(((($o&5)==1))&&echo YES||echo "NO ") ";
echo "| |";
echo -n "| | $o & 5 >= 1 ? $(((($o&5)>=1))&&echo YES||echo "NO ") ";
echo "| ==> $o & 5 > 0 ? $(((($o&5)>0))&&echo YES||echo "NO ") |";
echo "|=========================================================|";
echo ;
done
echo ;
)


Hello World !

... 或:“Rise and Shine!”

So how do we do it now?! Let's imagine, we have the following option set:

# option set:
option_1=1
option_2=2
option_3=4
option_4=8
option_5=16
option_6=32
option_7=64
option_8=128
option_9=256


We could for example set a selection of those options in a function and return the combined code as a return value. Or the other way round: pass a selection of options as one numeric parameter. Whatever your use case is, the options would be summed together:

# set options:
option = option_1
+ option_4
+ option_5
+ option_9


How do we best check, which options are set (1,4,5, & 9)? It depends of course again on your use case, but i kinda like the case construct! Something like ...

case $option in
0) echo "no option set!";;
1) echo "option 1";;
2) echo "option 2";;
3) echo "option 1 and 2";;
*) echo "...";;
esac

could work, but is not very nice, as we would have to construct every combination!


获胜者是...

... 或:“真实的案例

We can use case in the following way instead ...

echo "check for options using >0:"
case 1 in
$(( (option & option_1) >0 )) ) echo "- option_1 is set";;&
$(( (option & option_2) >0 )) ) echo "- option_2 is set";;&
$(( (option & option_3) >0 )) ) echo "- option_3 is set";;&
$(( (option & option_4) >0 )) ) echo "- option_4 is set";;&
$(( (option & option_5) >0 )) ) echo "- option_5 is set";;&
$(( (option & option_6) >0 )) ) echo "- option_6 is set";;&
$(( (option & option_7) >0 )) ) echo "- option_7 is set";;&
$(( (option & option_8) >0 )) ) echo "- option_8 is set";;&
$(( (option & option_9) >0 )) ) echo "- option_9 is set";;&
esac

Please note that

  • the spaces within $(( (option & option_1) >0 )) ) are completely optional and are added for readability. The last closing bracket ) is for the case construct.
  • the command-lists are terminated with ;;&, in order to continue evaluation with the next option test. In case you want to abord further processing of the list, set option=0 or to your current option=option_X.

... we get the following result:

check for options using >0:
- option_1 is set
- option_4 is set
- option_5 is set
- option_9 is set

欢呼! :-)


如果我是个有钱人!

echo "# to use it in an if statement:";
if (((option&option_5)>0)); then
echo "- option_5 is set"
else
echo "- option_5 is NOT set"
fi

# to use it in an if statement:
- option_5 is set


但最后穷人的情况:

echo "# or to use it just for conditional execution:";
(((option&option_6)>0)) \
&& echo "- option_6 is set" \
|| echo "- option_6 is NOT set"

# or to use it just for conditional execution:
- option_6 is NOT set


“不回答问题的人通过了考试。”

... 或者:“留下来,”他说,“那只是一个测试。”

(卡夫卡,1975:181)

So it would be perfectly possible to use the solution as posted in the question, by the slight change as follows:

(
declare -A releases=([token]=4)
declare -i MASK=5

if [[ $(( releases["token"] & $MASK )) -gt 0 ]]; then
echo YES
else
echo NO
fi
)

The changes are the following: - use $(()) instead of () to do the test, which will return the value of the bitwise comparison - use -gt 0 instead of -eq 1


完全在行动:

( # start a sub-shell to encapsulate vars for easy copy-paste into the terminal
# do not use this in a script!
echo ;

for ((i=0; i<9;i++)); do
o="option_$((i+1))"
declare -i $o=$((1<<$i))
echo "$o = ${!o}"
done

echo ;
echo ;

echo "# set options:"
echo "option = option_1"
echo " + option_4"
echo " + option_5"
echo " + option_9"

option=option_1+option_4+option_5+option_9

echo ;
echo ;

echo "check for options using >0:"
case 1 in
$(( (option & option_1) >0 )) ) echo "- option_1 is set";;&
$(( (option & option_2) >0 )) ) echo "- option_2 is set";;&
$(( (option & option_3) >0 )) ) echo "- option_3 is set";;&
$(( (option & option_4) >0 )) ) echo "- option_4 is set";;&
$(( (option & option_5) >0 )) ) echo "- option_5 is set";;&
$(( (option & option_6) >0 )) ) echo "- option_6 is set";;&
$(( (option & option_7) >0 )) ) echo "- option_7 is set";;&
$(( (option & option_8) >0 )) ) echo "- option_8 is set";;&
$(( (option & option_9) >0 )) ) echo "- option_9 is set";;&
esac

echo ;
echo ;

echo "# to use it in an if statement:";
echo " => if (((option&option_5)>0));";
if (((option&option_5)>0)); then
echo "- option_5 is set"
else
echo "- option_5 is NOT set"
fi

echo ;
echo ;

declare -A releases=([token]=4)
declare -i MASK=5

echo "# Does 4 pass the mask 5 in the test construct?";
echo " => if [[ $(( releases["token"] & $MASK )) -gt 0 ]];";
if [[ $(( releases["token"] & $MASK )) -gt 0 ]]; then
echo YES
else
echo NO
fi

echo ;
echo ;

echo "# or to use it just for conditional execution:";
echo " => (((option&option_6)>0)) && do || dont";
(((option&option_6)>0)) \
&& echo "- option_6 is set" \
|| echo "- option_6 is NOT set"

)


结束

Exit 0

关于bash - 如何在 if 语句中使用位运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14318451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com