Binary digits: Difference between revisions

Content added Content deleted
({{header|ZX Spectrum Basic}})
Line 8: Line 8:


The results can be achieved using builtin radix functions within the language, if these are available, or a user defined function can be utilized. The output produced should consist just of the binary digits. These should be no whitespace, radix or sign markers in the produced output, and superfluous leading zeros should not appear in the results.
The results can be achieved using builtin radix functions within the language, if these are available, or a user defined function can be utilized. The output produced should consist just of the binary digits. These should be no whitespace, radix or sign markers in the produced output, and superfluous leading zeros should not appear in the results.

=={{header|ZX Spectrum Basic}}==

<lang basic>10 LET n=5: GO SUB 1000: PRINT s$
20 LET n=50: GO SUB 1000: PRINT s$
30 LET n=9000: GO SUB 1000: PRINT s$
999 STOP
1000 REM convert to binary
1010 LET t=n: REM temporary variable
1020 LET s$="": REM this will contain our binary digits
1030 LET sf=0: REM output has not started yet
1040 FOR l=126 TO 1 STEP -1
1050 LET d$="0": REM assume next digit is zero
1060 IF t > (2^l) THEN LET d$="1": LET t=t-(2^l): LET sf=1
1070 IF (sf <> 0) THEN LET s$=s$+d$
1080 NEXT l
1090 RETURN</lang>



[[Category:Basic language learning]]
[[Category:Basic language learning]]