还是只用于正数, 位运算实现, 只显示有效数字- @echo off & setlocal enableDelayedExpansion
- REM 2147483647=~(1<<31)
- set HEX_TAB=0123456789ABCDEF
- for /l %%a in () do (
- set /p num="Input a decimal number: "
-
- <nul set /p="Binary: "
- set code=
- set /a "test=1<<31
- for /l %%i in (31 -1 0) do (
- set /a "bit=num&test,test>>=1,test&=~(1<<31),bit>>=%%i,bit&=~(1<<31)"
- set "code=!code!!bit!"
- if !code!==0 set code=
- )
- echo !code!
-
- <nul set /p="Hex.: "
- set code=
- set /a "test=0xf<<4*(32/4-1)
- for /l %%i in (28 -4 0) do (
- set /a "bit=num&test,test>>=4,test&=(1<<28)-1,bit>>=%%i,bit&=(1<<28)-1"
- for %%p in (!bit!) do set "code=!code!!HEX_TAB:~%%p,1!"
- if !code!==0 set code=
- )
- echo !code!
-
- <nul set /p="Octal: "
- set code=
- set /a "test=07<<3*(32/3)" & rem 32=3*10+2
- for /l %%i in (30 -3 0) do ( rem Arithmetic Shift Right >>
- set /a "bit=num&test,test>>=3,test&=(1<<30)-1,bit>>=%%i,bit&=(1<<30)-1"
- set "code=!code!!bit!"
- if !code!==0 set code=
- )
- echo !code! & echo.
- )
- exit /b
复制代码
|