1) there are 6 numbers 1,2,4,8,16,32,64. Increase by power 2.

2) given a number N <=64, for example 59, see what numbers in 1) consist of 59.

3) a number can be used only once.

It's better to code in DOS shell.

For example, 59 = 32+16+8+2+1

2) given a number N <=64, for example 59, see what numbers in 1) consist of 59.

3) a number can be used only once.

It's better to code in DOS shell.

For example, 59 = 32+16+8+2+1

The algorithm is standard binary coding, because of "power 2" in point 1. Why to obscure that fact? Sounds like homework ...

jl66

There is a limit in DOS when performing integer maths. The solution provided uses mathematical evaluations to arrive at the output.And as you've discovered, this noly works upto a certain limit.

This modified loop1 will handle numbers upto 2,147,483,647.

@echo off

if "%1"=="" exit /b

if %1 leq 0 exit /b

set result=

set dec=%1

set bit=1

set tmp=1

:loop1

set /a tmp=tmp*2

if %tmp% gtr %dec% goto end_loop1

set bit=%tmp%

goto loop1

:end_loop1

:loop2

if %bit% leq %dec% (

set /a dec-=bit

if not defined result (

set result=%bit%

) else (

set result=%result%+%bit%

)

)

set /a bit=bit/2

if %bit% gtr 0 goto loop2

echo %1 = %result%

exit /b

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

@echo off

if "%1"=="" exit /b

if %1 leq 0 exit /b

set dec=%1

set result=

set bit=1

:loop1

set /a bit=bit*2

if %bit% leq %dec% goto loop1

set /a bit=bit/2

:loop2

if %bit% leq %dec% (

set /a dec-=bit

if not defined result (

set result=%bit%

) else (

set result=%result%+%bit%

)

)

set /a bit=bit/2

if %bit% gtr 0 goto loop2

echo %1 = %result%