You can do bit math and compare everything in one statement. The trick is to deliberately create a division by zero error if the result is what you are looking for. Of course stderr should be redirected to nul, and the operator || used to check for error conditions (indicating TRUE).
This method 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 was never said to be somewhat obvious: special characters such as & and ) must either be escaped or specified in the SET / A calculation. I deliberately demonstrated both techniques in the above example.
EDIT:. The logic can be made even simpler by changing 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 )
source share