linux shell实现位运算
linux shell貌似不支持位运算,所以自己写了一个位与的实现,以后谁要用可以拿去,实现如下:
function yu() { bit1=`echo "obase=2;$1" | bc` bit2=`echo "obase=2;$2" | bc` len1=${#bit1} len2=${#bit2} if [ "$len1" -gt "$len2" ] then len=$len1 bit2=`printf "%0${len}d" $bit2` #echo $bit2 elif [ "$len1" -lt "$len2" ] then len=$len2 bit1=`printf "%0${len}d" $bit1` #echo $bit1 else len=$len1 fi for ((i=1;i<=len;i++)) do b1=${bit1:0-$i:1} b2=${bit2:0-$i:1} if [ $b1 -eq 1 ] && [ $b2 -eq 1 ] then b=1 else b=0 fi result=`echo "$b""$result"` done result=`echo "obase=10;ibase=2;$result" | bc` echo $result } a=5 b=2 echo end=`yu $a $b`