Skip to main content

How to Check if a Number is a Power of Two in Batch Script

A Power of Two is any number that can be expressed as $2^n$ (where $n$ is a non-negative integer). The sequence goes: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, and so on. These numbers are foundational in computing, memory sizes, buffer allocations, and bit-level operations all revolve around powers of 2. In Batch, there are two ways to check: a "Loop Division" method and a clever "Bitwise AND" trick.

In this guide, we will demonstrate both approaches.

Method 1: The Bitwise AND Trick (Fastest)

A power of two in binary has exactly one bit set to 1 (e.g., 1000 = 8). The expression n AND (n - 1) will be zero only if n is a power of two.

Implementation Script

@echo off
setlocal enabledelayedexpansion

set /p "num=Enter a number: "

:: Validate: Must be a positive integer
if !num! LEQ 0 (
echo [NO] !num! is not a power of two.
pause
exit /b
)

:: Bitwise AND check: n & (n-1) == 0
set /a "check=num & (num - 1)"

if !check! EQU 0 (
echo [YES] !num! IS a power of two.
) else (
echo [NO] !num! is NOT a power of two.
)

pause
How the Bitwise Trick Works

A power of two in binary is a single 1 bit followed by zeros (e.g., 8 = 1000). Subtracting 1 flips that bit and sets all lower bits (e.g., 7 = 0111). The bitwise AND of these two values is always 0000. For any non-power-of-two, at least one bit survives the AND operation, producing a non-zero result.

Method 2: Repeated Division (Intuitive)

If you repeatedly divide the number by 2 and it always divides evenly (no remainder) until you reach 1, it is a power of two.

Implementation Script

@echo off
setlocal enabledelayedexpansion

set /p "num=Enter a number: "

if !num! LEQ 0 (
echo [NO] Not a power of two.
pause
exit /b
)

set "val=!num!"

:div_loop
if !val! EQU 1 (
echo [YES] !num! IS a power of two.
pause
exit /b
)

set /a "remainder=val %% 2"
if !remainder! NEQ 0 (
echo [NO] !num! is NOT a power of two.
pause
exit /b
)

set /a "val/=2"
goto :div_loop
note

The repeated division method is more intuitive but slower for large numbers. It requires up to 30 iterations (one per bit) to verify the largest valid power of two ($2^30$), while the bitwise AND trick always completes in a single arithmetic operation.

Why Check for Powers of Two?

  1. Memory Allocation: Verifying that a buffer size or partition size is a "clean" power of 2 (like 256, 512, 1024) before passing it to a system utility.
  2. Bit Manipulation: If a value is a power of two, it represents a single active flag in a bitmask, which simplifies debugging bitwise settings.
  3. Performance Tuning: Many algorithms run fastest when the input size is a power of two (e.g., FFT, hash table sizes).

Important Considerations

Zero

Zero is not a power of two, even though 0 & (0 - 1) evaluates to 0. Both scripts guard against this by rejecting values less than or equal to 0 before performing the check.

32-Bit Overflow

The largest power of two in 32-bit signed integers is $2^30 = 1,073,741,824$. The value $2^31$ is 2,147,483,648, which overflows to a negative number in Batch's signed 32-bit arithmetic and will be incorrectly rejected by the positive-number validation.

Negative Numbers

Negative numbers are never powers of two. Both scripts handle this by checking if !num! LEQ 0 at the start and exiting early with a clear message.

Conclusion

Checking for powers of two is a quick, elegant operation that connects your Batch scripts to the fundamental principles of computing. The bitwise AND trick (n & (n-1) == 0) is one of the most efficient single-line checks in all of programming. By mastering this technique, you gain the ability to validate memory configurations, optimize algorithm inputs, and debug bitmask flags with absolute precision.