How to Perform Bitwise Operations (AND, OR, XOR) in Batch Script
Bitwise operations are advanced mathematical operations that work directly on the binary (bit-by-bit) representation of numbers. While less common than standard arithmetic, they are extremely powerful for certain tasks, such as managing option flags, working with low-level data, or performing highly efficient integer math. The Windows Batch interpreter provides a full set of bitwise operators directly within its arithmetic engine, the SET /A command.
This guide will teach you how to use the bitwise AND, OR, XOR, NOT, and Shift operators in your batch scripts. You will learn the syntax for each and see practical examples of how to use them to set, clear, and check for specific flags within a single number.
What are Bitwise Operations?
Instead of treating the number 10 as a decimal value, bitwise operations treat it as a sequence of bits: 1010. They then perform logical comparisons or shifts on each of these individual bits. This makes them ideal for situations where each bit in a number represents a separate true/false option or "flag."
The Core Command: SET /A
All bitwise operations in batch scripting are handled by the SET /A command. This command evaluates the expression inside its quotes using a powerful integer math parser.
The syntax is: SET /A "result = number1 operator number2"
The Bitwise Operators Explained
The following operators are available within a SET /A expression.
| Operator | Name | Description | Example (Binary) |
|---|---|---|---|
& | Bitwise AND | Returns a 1 in each bit position where both numbers have a 1. | 1100 & 1010 = 1000 |
| **` | `** | Bitwise OR | Returns a 1 in each bit position where either number has a 1. |
^ | Bitwise XOR | (Exclusive OR) Returns a 1 if the bits are different. | 1100 ^ 1010 = 0110 |
~ | Bitwise NOT | Inverts all the bits of a single number. (A unary operator). | ~...00001010 = ...11110101 |
<< | Left Shift | Moves all bits to the left by a specified amount. | 1100 << 1 = 11000 |
>> | Right Shift | Moves all bits to the right by a specified amount. | 1100 >> 1 = 0110 |
Basic Example: Using the Operators
This script demonstrates each operator with the decimal numbers 12 (1100 in binary) and 10 (1010 in binary).
@ECHO OFF
SET "A=12"
SET "B=10"
ECHO --- Bitwise Operations ---
ECHO A = %A% (1100), B = %B% (1010)
ECHO.
SET /A "ResultAND = %A% & %B%"
ECHO A AND B (&) = %ResultAND% (Binary 1000 = 8)
SET /A "ResultOR = %A% | %B%"
ECHO A OR B (|) = %ResultOR% (Binary 1110 = 14)
SET /A "ResultXOR = %A% ^ %B%"
ECHO A XOR B (^) = %ResultXOR% (Binary 0110 = 6)
SET /A "ResultLSH = %A% << 1"
ECHO A << 1 = %ResultLSH% (Binary 11000 = 24)
SET /A "ResultRSH = %A% >> 1"
ECHO A >> 1 = %ResultRSH% (Binary 0110 = 6)
Practical Use Case 1: Checking for a Specific Flag with AND (&)
This is the most common use for bitwise operations. Imagine you have an "options" variable where each bit represents a setting.
- Bit 0 (Value 1): Option A is ON
- Bit 1 (Value 2): Option B is ON
- Bit 2 (Value 4): Option C is ON
- Bit 3 (Value 8): Option D is ON
If Options=5, that's 0101 in binary, meaning Options A and C are ON. How do you check if Option C (value 4) is enabled? You use a bitwise AND.
@ECHO OFF
SET "Options=5"
SET "FLAG_C=4"
REM --- Check if the FLAG_C bit is set in the Options variable ---
SET /A "Check = %Options% & %FLAG_C%"
ECHO Options value is %Options%. Checking for Flag C (value %FLAG_C%)...
IF %Check% EQU %FLAG_C% (
ECHO [SUCCESS] Option C is enabled.
) ELSE (
ECHO [FAILURE] Option C is disabled.
)
Practical Use Case 2: Setting and Clearing Flags with OR (|) and AND NOT (& ~)
Using the same Options variable, you can safely turn flags on or off without disturbing the other flags.
To SET a flag (turn it ON): Use Bitwise OR (|).
SET "Options=5" REM 0101
SET "FLAG_B=2" REM 0010
REM --- Turn on Option B ---
SET /A "Options = %Options% | %FLAG_B%"
ECHO New Options value is: %Options% (Result is 7, which is 0111 in binary)
This correctly turns on the second bit without affecting the others.
To CLEAR a flag (turn it OFF): Use Bitwise AND NOT (& ~).
SET "Options=5" REM 0101
SET "FLAG_A=1" REM 0001
REM --- Turn off Option A ---
SET /A "Options = %Options% & ~%FLAG_A%"
ECHO New Options value is: %Options% (Result is 4, which is 0100 in binary)
This is an advanced trick. ~1 creates a mask with all bits set to 1 except the last one. The AND operation then preserves all original bits but forces the last one to 0.
Common Pitfalls and How to Solve Them
The bitwise operators &, |, and ^ are also special characters in the cmd.exe shell.
&: Command separator.|: Pipe operator.^: Escape character.
Solution: This is why the SET /A "..." syntax is so important. When your expression is enclosed in double quotes, the cmd.exe parser hands the entire string to the arithmetic engine, which correctly interprets these characters as bitwise operators instead of shell operators. You do not need to escape them inside the quotes.
Example of script with error:
REM This will FAIL. The '^' is treated as an escape character.
SET /A Result = 12 ^ 10
Correct Syntax
REM This works perfectly.
SET /A "Result = 12 ^ 10"
Conclusion
The SET /A command provides a complete suite of bitwise operators for low-level integer manipulation in batch scripts.
- The primary use case is for managing option flags, where each bit of a number represents a separate true/false state.
- Use
&(AND) to check if a flag is set. - Use
|(OR) to set a flag. - Use
& ~(AND NOT) to clear a flag. - Always enclose your
SET /Aexpression in double quotes to ensure special characters like&,|, and^are interpreted correctly as bitwise operators.