goulashsoup
goulashsoup

Reputation: 3076

Batch - Meaning of escaped vertical bar | before equal sign =

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.

Upvotes: 1

Views: 407

Answers (1)

SomethingDark
SomethingDark

Reputation: 14305

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.

Upvotes: 5

Related Questions