Binary digits: Difference between revisions

From Rosetta Code
Content added Content deleted
m (fixed typing error)
(Newline is whitespace too so ...)
Line 7: Line 7:
The decimal value 9000 should produce an output of 10001100101000
The decimal value 9000 should produce an output of 10001100101000


The results can be achieved using builtin radix functions within the language, if these are available, or alternatively a user defined function can be utilized. The output produced should consist just of the binary digits. There 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 alternatively a user defined function can be utilized. The output produced should consist just of the binary digits of each number followed by a newline. There should be no other whitespace, radix or sign markers in the produced output, and superfluous leading zeros should not appear in the results.


=={{header|Java}}==
=={{header|Java}}==

Revision as of 20:40, 6 July 2011

Binary digits is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The task is to output the set of binary digits for a given non negative decimal integer.

The decimal value 5, should produce an output of 101 The decimal value 50 should produce an out of 110010 The decimal value 9000 should produce an output of 10001100101000

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

Java

<lang java>public class Main {

   public static void main(String[] args)
   {
       System.out.println(Integer.toBinaryString(5));
       System.out.println(Integer.toBinaryString(50));
       System.out.println(Integer.toBinaryString(9000));
   }

}</lang> Output:

101
110010
10001100101000

PHP

<lang php><?php echo decbin(5); echo decbin(50); echo decbin(9000);</lang> Output:

101
110010
10001100101000

Python

<lang python>>>> for i in range(16): print('{0:b}'.format(i))

0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111</lang>

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 0 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>