1

I am currently struggling understanding a line of an introduction to windows batch scripting:

SET /A errno=0
SET /A ERROR_SOMECOMMAND_NOT_FOUND=2

... 
... SET /A errno^|=%ERROR_SOMECOMMAND_NOT_FOUND%

According to this answer the circumflex ^ is an escape character, so we are ending up with errno|=%ERROR_SOMECOMMAND_NOT_FOUND%. But then what is this code doing?

In the according article the author states that this gives the flexibility to bitwise OR multiple error numbers together.

Ok, but I couldn't find any article about bitwise operations in batch with a line like above...

So please, enlight me a little.

1 Answer 1

5

As the paragraph above the code in question states, this is a bitwise OR operator. It is used to set multiple binary flags simultaneously.

In the code

SET /A ERROR_HELP_SCREEN=1
SET /A ERROR_SOMECOMMAND_NOT_FOUND=2
SET /A ERROR_OTHERCOMMAND_FAILED=4

ERROR_HELP_SCREEN is 0b001
ERROR_SOMECOMMAND_NOT_FOUND is 0b010
ERROR_OTHERCOMMAND_FAILED is 0b100

Using a bitwise OR will allow you to return something like 0b101, which would mean that an other command failed and a help screen error was raised.

The ^ is necessary because batch scripts treat | like pipes regardless of context, so SET /A errno|=%ERROR_OTHERCOMMAND_FAILED% will throw a syntax error even though it's perfectly valid on the command line.

1
  • 4
    Just to add to this: when quoting the set /A "var=value" pair you don't need to escape and with set /A you don't need to include the percent signs for variable names SET /A "errno|=ERROR_SOMECOMMAND_NOT_FOUND"
    – user6811411
    Commented May 16, 2018 at 15:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.