Binary digits: Difference between revisions
Content deleted Content added
Add Mercury. |
Add Batch File solution |
||
Line 104: | Line 104: | ||
return outstr |
return outstr |
||
}</lang> |
}</lang> |
||
=={{header|Batch File}}== |
|||
This num2bin.bat file handles non-negative input as per the requirements with no leading zeros in the output. Batch only supports signed integers. This script also handles negative values by printing the appropriate two's complement notation. |
|||
<lang dos> |
|||
@echo off |
|||
:num2bin IntVal [RtnVar] |
|||
setlocal enableDelayedExpansion |
|||
set /a n=%~1 |
|||
set rtn= |
|||
for /l %%b in (0,1,31) do ( |
|||
set /a "d=n&1, n>>=1" |
|||
set rtn=!d!!rtn! |
|||
) |
|||
for /f "tokens=* delims=0" %%a in ("!rtn!") do set rtn=%%a |
|||
(endlocal & rem -- return values |
|||
if "%~2" neq "" (set %~2=%rtn%) else echo %rtn% |
|||
) |
|||
exit /b |
|||
</lang> |
|||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |