Jump to content

Binary digits: Difference between revisions

m
Line 559:
10001100101000
</pre>
 
==={{header|BBC BASIC}}===
<lang bbcbasic> FOR num% = 0 TO 16
PRINT FN_tobase(num%, 2, 0)
NEXT
END
REM Convert N% to string in base B% with minimum M% digits:
DEF FN_tobase(N%,B%,M%)
LOCAL D%,A$
REPEAT
D% = N%MODB%
N% DIV= B%
IF D%<0 D% += B%:N% -= 1
A$ = CHR$(48 + D% - 7*(D%>9)) + A$
M% -= 1
UNTIL (N%=FALSE OR N%=TRUE) AND M%<=0
=A$</lang>
The above is a generic "Convert to any base" program.
Here is a faster "Convert to Binary" program:
<lang bbcbasic>PRINT FNbinary(5)
PRINT FNbinary(50)
PRINT FNbinary(9000)
END
 
DEF FNbinary(N%)
LOCAL A$
REPEAT
A$ = STR$(N% AND 1) + A$
N% = N% >>> 1 : REM BBC Basic prior to V5 can use N% = N% DIV 2
UNTIL N% = 0
=A$</lang>
 
==={{header|Commodore BASIC}}===
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.