windows 如何在 bat 文件中按位制作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/439038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:49:00  来源:igfitidea点击:

How do I make a bitwise and in a bat-file?

windowsbatch-filecmdbit-manipulation

提问by loraderon

I have tried with the following, but it just says "& was unexpected at this time."

我已经尝试了以下内容,但它只是说“&此时出乎意料”。

@echo off
:enter-input
echo Please enter a number between 1 and 15:
echo 1 = Selection one
echo 2 = Selection two
echo 4 = Selection three
echo 8 = Selection four
echo x = Quit

set INPUT=
set /P INPUT=Type number: %=%

if "%INPUT%" == "" goto enter-input
if "%INPUT%" == "x" goto end
if "%INPUT%" == "X" goto end

set /A %INPUT%
if %INPUT% & 1 == 1 echo Selection one
if %INPUT% & 2 == 2 echo Selection two
if %INPUT% & 4 == 4 echo Selection three
if %INPUT% & 8 == 8 echo Selection four

echo Done
:end

回答by dbenham

It is possible to do the bit-wise math and the comparison all in one statement. The trick is to intentionally create a divide by zero error if the result is what you are looking for. Of course stderr should be redirected to nul, and the ||operator is used to test for the error condition (indicating TRUE).

可以在一个语句中进行按位数学运算和比较。诀窍是如果结果是您正在寻找的结果,则有意地创建除以零错误。当然stderr应该重定向到nul,用||操作符来测试错误条件(表示TRUE)。

This technique eliminates the need for any intermediate variable.

这种技术消除了对任何中间变量的需要。

@echo off
:enter-input
set "input="
echo(
echo Please enter a number between 1 and 15:
echo 1 = Selection one
echo 2 = Selection two
echo 4 = Selection three
echo 8 = Selection four
echo x = Quit

set /P INPUT=Type number:

if not defined input goto enter-input
if /i "%input%" == "X" exit /b

2>nul (
  set /a "1/(1-(input&1))" || echo Selection one
  set /a "1/(2-(input&2))" || echo Selection two
  set /a 1/(4-(input^&4^)^) || echo Selection three
  set /a 1/(8-(input^&8^)^) || echo Selection four
)
pause
goto enter-input

The accepted answer never stated the somewhat obvious: Special characters like &and )must be either escaped or quoted within the SET /A computation. I intentionally demonstrated both techniques in the example above.

接受的答案从未说明过一些明显的问题:像&和 之类的特殊字符)必须在 SET /A 计算中进行转义或引用。我有意在上面的示例中演示了这两种技术。



EDIT:The logic can be made even simpler by reversing the logic (divide by zero if false) and using the &&operator.

编辑:通过反转逻辑(如果为假则除以零)并使用&&运算符,可以使逻辑更简单。

2>nul (
  set /a "1/(input&1)" && echo Selection one
  set /a "1/(input&2)" && echo Selection two
  set /a 1/(input^&4^) && echo Selection three
  set /a 1/(input^&8^) && echo Selection four
)

回答by loraderon

I found a way of doing this.

我找到了一种方法来做到这一点。

@echo off
:enter-input
echo Please enter a number between 1 and 15:
echo 1 = Selection one
echo 2 = Selection two
echo 4 = Selection three
echo 8 = Selection four
echo x = Quit

set /P INPUT=Type number:

if "%INPUT%" == "" goto enter-input
if "%INPUT%" == "x" goto end
if "%INPUT%" == "X" goto end

set /A isOne = "(%INPUT% & 1) / 1"
set /A isTwo = "(%INPUT% & 2) / 2"
set /A isThree = "(%INPUT% & 4) / 4"
set /A isFour = "(%INPUT% & 8) / 8"

if %isOne% == 1 echo Selection one
if %isTwo% == 1 echo Selection two
if %isThree% == 1 echo Selection three
if %isFour% == 1 echo Selection four

echo Done
:end

回答by nusi

SET LEFT = 1
SET RIGHT = 2
SET /A RESULT = %LEFT% & %RIGHT%

Please escape the ampersand character (&) with a '^' if you try it directly in cmd.exe.

如果您直接在 cmd.exe 中尝试,请使用 '^' 将 & 字符 (&) 转义。

回答by Zimba

For bitwise AND, put expression in quotes to avoid error
eg. set /a "48 & 23"

对于按位与,将表达式放在引号中以避免错误,
例如。set /a "48 & 23"