This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
102} How can I extract the individual bits from a decimal byte?
Basically this is the same task as the decimal to
binary conversion in
item #32.
The formulation below emphasizes extracting each bit of a decimal byte
individually. A byte contains 8 bits, usually labeled from 0 to 7. It
also shows that are many different options to perform a task since the
solution here differs considerably from the one used in item #32.
These bitwise operations were the key elements of
programming back in the days of Vic 20 and Commodore 64, but can still
arise in the present day as seen in the references.
@echo off & setlocal enableextensions enabledelayedexpansion
set decimal_=%1
set /a b0=%decimal_% ^& 1
set /a b1=%decimal_% ^& 2
set /a b2=%decimal_% ^& 4
set /a b3=%decimal_% ^& 8
set /a b4=%decimal_% ^& 16
set /a b5=%decimal_% ^& 32
set /a b6=%decimal_% ^& 64
set /a b7=%decimal_% ^& 128
set i=-1
for %%b in (!b0! !b1! !b2! !b3! !b4! !b5! !b6! !b7!) do (
set bit=%%b
set /a i+=1
if !bit! EQU 0 (echo bit !i! = 0) else (echo bit !i! = 1)
)
endlocal & goto :EOF
An example of the output
C:\_D\TEST>cmdfaq 12
bit 0 = 0
bit 1 = 0
bit 2 = 1
bit 3 = 1
bit 4 = 0
bit 5 = 0
bit 6 = 0
bit 7 = 0
Examples of bitwise operations
@echo off & setlocal enableextensions
set dec1_=121
set dec2_=145
::
set /a decand_=%dec1_% ^& %dec2_%
echo %dec1_% AND %dec2_% = %decand_%
::
set /a decor_=%dec1_% ^| %dec2_%
echo %dec1_% OR %dec2_% = %decor_%
::
set /a decxor_=%dec1_% ^^ %dec2_%
echo %dec1_% XOR %dec2_% = %decxor_%
::
set /a decnot_=%dec1_% ^^ 255
echo NOT %dec1_% = %decnot_%
::
set dec3_=1
set shft_=3
set decshl_=%dec3_%
set /a decshl_ ^<^<= %shft_%
echo %dec3_% SHL %shft_% = %decshl_%
::BR>
set dec4_=128
set shft_=1
set decshr_=%dec4_%
set /a decshr_ ^>^>= %shft_%
echo %dec4_% SHR %shft_% = %decshr_%
::
endlocal & goto :EOF
The output will be
C:\_D\TEST>cmdfaq
121 AND 145 = 17
121 OR 145 = 249
121 XOR 145 = 232
NOT 121 = 134
1 SHL 3 = 8
128 SHR 1 = 64
The SHL (shift left) can be used to get the powers of 2.
The above could also be written as
@echo off & setlocal enableextensions
set dec1_=121
set dec2_=145
::
set /a decand_=%dec1_% "&" %dec2_%
echo %dec1_% AND %dec2_% = %decand_%
::
set /a decor_=%dec1_% "|" %dec2_%
echo %dec1_% OR %dec2_% = %decor_%
::
set /a decxor_=%dec1_% "^" %dec2_%
echo %dec1_% XOR %dec2_% = %decxor_%
::
set /a decnot_=%dec1_% "^" 255
echo NOT %dec1_% = %decnot_%
::
set dec3_=1
set shft_=3
set decshl_=%dec3_%
set /a decshl_ "<<"= %shft_%
echo %dec3_% SHL %shft_% = %decshl_%
::
set dec4_=128
set shft_=1
set decshr_=%dec4_%
set /a decshr_ ">>"= %shft_%
echo %dec4_% SHR %shft_% = %decshr_%
::
endlocal & goto :EOF
The SET /A operations include e.g.
! Unary NOT operator
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise OR
%% Remainder (modulo) operator
>> Logical shift right
<< Logical shift left