Background:
You have been given an assignment to convert base 10 numbers to base 2 without using any premade base conversion functions. You can't use any imported libraries either.
Problem:
Convert an input string from base 10 (decimal) to base 2 (binary). You may not use any premade base conversion code/functions/methods, or imported libraries. Since this is code-golf, the shortest answer in bytes will win.
Input will be anything from -32768 to 32767 (include sign byte handling in your code)
32 Answers 32
JavaScript, 46
for(x=prompt(o='');x;x>>>=1)o=(x&1)+o;alert(o)
-
\$\begingroup\$ Lol, I didn't even know that a 4-character operator (
>>>=) existed! +1 (Also, if you run it in the console, you can save the last 9 characters.) \$\endgroup\$Doorknob– Doorknob2014年02月16日 03:53:26 +00:00Commented Feb 16, 2014 at 3:53 -
1\$\begingroup\$ It's not 4-characters, it's two operators: the >>> is the 0-filling bitwise right shift, followed by an assignment. Try:
x=8; x>>>=1; x;andx=8; x>>>1; x;-- in the first case, the value of x has changed; in the second, it has not. \$\endgroup\$Graham Charles– Graham Charles2014年02月16日 09:03:40 +00:00Commented Feb 16, 2014 at 9:03 -
3\$\begingroup\$ @GrahamCharles
>>>=is a single operator. \$\endgroup\$primo– primo2014年02月16日 13:43:12 +00:00Commented Feb 16, 2014 at 13:43 -
\$\begingroup\$ Well, look at that! Thanks, @primo... you learn something every day! \$\endgroup\$Graham Charles– Graham Charles2014年02月17日 23:52:52 +00:00Commented Feb 17, 2014 at 23:52
-
2\$\begingroup\$ @ComFreek That would reverse the order of the digits \$\endgroup\$copy– copy2014年02月19日 18:39:05 +00:00Commented Feb 19, 2014 at 18:39
Brainf*ck, (削除) 98 (削除ここまで) 77
Obviously this isn't for the purpose of winning but what would a competition be if it didnt have a brainfk solution
++++[>++++<-]>>,<[->>++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]++++++[->++++++++<]>.[-]>[-<<<+>>>]<<<<]
Since brainfk can only handle 8bit integers and no negatives I guess it doesn't fully abide by the rules but hey I was never in it to win it.
This actually does work for 16-bit input if your interpreter supports
I even got it to output in ascii values
Here is the annotated code:
++[>++++<-] preload 8 onto cell 1
>>,< input into cell 2
[- iterate over cell 1
>>++< put 2 in cell 3
[->-[>+>>]>[+[-<+>]>+>>]<<<<<] division algorithm: converts {n d} into {0 d_minus_n%d n%d n/d}
>[-]++++++[->++++++++<]> clears cell 4 and puts 48(ascii of 0) into cell 5
.[-] output n%2 and clear it (the bit)
>[-<<<+>>>] bring n/2 into cell 2 (to be used for division in next iteration)
<<<<] end iterate
Shorter algorithm (77):
+>,>-<[>>[->]++[-<+]-<-]++++++++[->++++++<]>+[->+>+>+>+>+>+>+>+<<<<<<<<]>[.>]
This one can only handle 8bit integers.
The algorithm works by using a binary counter which is actually very short (one increment is >[->]++[-<+]-<- which then lays out the bits. The issue is that it is difficult to print out all of the bits
That last algorithm can be adapted to fit any number of bits at the expense of bytes. To be able to deal with N bit integers, it requires 53+3*N bytes to encode.
examples:
(1 bit) +>,>-<[>>[->]++[-<+]-<-]++++++++[->++++++<]>+[->+<]>[.>]
(2 bit) +>,>-<[>>[->]++[-<+]-<-]++++++++[->++++++<]>+[->+>+<<]>[.>]
(3 bit) +>,>-<[>>[->]++[-<+]-<-]++++++++[->++++++<]>+[->+>+>+<<<]>[.>]
etc
GolfScript - 17 bytes
~{.1&2円/}16*;]-1%
Not too much more verbose than the built-in ~2base.
-
1\$\begingroup\$ I don't know golfscript but a few sample runs lead me to conclude that you should remove the
~\$\endgroup\$user12205– user122052014年02月16日 22:10:39 +00:00Commented Feb 16, 2014 at 22:10 -
\$\begingroup\$ @ace Because the initial input is a string
"37", for example, the operation"37" & 1(in infix) is a set-wise operation. The~at the front converts the input to an integer. \$\endgroup\$primo– primo2014年02月17日 02:38:04 +00:00Commented Feb 17, 2014 at 2:38 -
\$\begingroup\$ I did my test here golfscript.apphb.com/… does this mean this interpreter is incorrect? (Sorry I really know nothing about golfscript) \$\endgroup\$user12205– user122052014年02月17日 08:19:05 +00:00Commented Feb 17, 2014 at 8:19
-
2\$\begingroup\$ The interpreter is correct; because you've pushed the integer value
10onto the stack, it is not necessary to evaluate it. However, when read fromstdin, the input will be a string (test here). The problem description also explicitly states that the input is a string. \$\endgroup\$primo– primo2014年02月17日 08:27:43 +00:00Commented Feb 17, 2014 at 8:27
Mandatory APL answer - 21 (削除) 22 (削除ここまで)
"01"[1+2|⌊⎕÷2⋆⊖0,⍳15]
Examples:
"01"[1+2|⌊⎕÷2⋆⊖0,⍳15]
⎕: 0
0000000000000000
"01"[1+2|⌊⎕÷2⋆⊖0,⍳15]
⎕: 13
0000000000001101
"01"[1+2|⌊⎕÷2⋆⊖0,⍳15]
⎕: 9999
0010011100001111
"01"[1+2|⌊⎕÷2⋆⊖0,⍳15]
⎕: -3
1111111111111101
"01"[1+2|⌊⎕÷2⋆⊖0,⍳15]
⎕: 32767
0111111111111111
-
\$\begingroup\$ You can reduce with almost 50% by using
⎕IO←0, and returning an array of bits instead of a string:2|⌊⎕÷2*⊖⍳16. \$\endgroup\$Adám– Adám2016年06月18日 23:09:03 +00:00Commented Jun 18, 2016 at 23:09
Turing Machine Code, 272 bytes
As usual, I'm using the rule table syntax defined here. You can test it on that site or, alternatively, using this java implementation.
A lot of the code is copied from my decimal-to-hex converter here.
0 * * l B
B * * l C
C * 0 r D
D * * r E
E * * r A
A _ * l 1
A * * r *
1 0 9 l 1
1 1 0 l 2
1 2 1 l 2
1 3 2 l 2
1 4 3 l 2
1 5 4 l 2
1 6 5 l 2
1 7 6 l 2
1 8 7 l 2
1 9 8 l 2
1 _ * r Y
Y * * * X
X * _ r X
X _ _ * halt
2 * * l 2
2 _ _ l 3
3 * 1 r 4
3 1 0 l 3
4 * * r 4
4 _ _ r A
Counts down from the input in base 10 while counting up from 0 in base 2. On decrementing zero, it erases the input block and terminates.
Dyalog APL, 11 bytes
2|⌊⎕÷2*⌽⍳16
2| The division remainder when halved of
⌊ the rounded down value of
⎕ the input
÷ divided by each of
2* two to the power of each of
⍳16 {0, 1, 2, ..., 15}
Requires ⎕IO←0 which is default on many systems.
Javascript 59
o='';i=parseInt(prompt());do{o=(i&1)+o}while(i>>=1)alert(o)
-
\$\begingroup\$ You can use
+xinstead ofparseInt(x)\$\endgroup\$Cyoce– Cyoce2016年08月07日 17:01:33 +00:00Commented Aug 7, 2016 at 17:01
Perl, 44
This is my first Perl program ever, so please forgive me if this can be easily golfed down further. Edit: Thank you @primo for taking 7 chars away from my answer.
$x=<>;do{@s=($x&1,@s)}while($x>>=1);print@s
(削除)
$x=<>;do{push@s,$x&1}while($x>>=1);print reverse@s
(削除ここまで)
The logic is essentially the same as my previous C solution.
Also, uses 64 bits.
-
1\$\begingroup\$ You can save the
reverseby constructing the array backwards:@s=($x&1,@s). \$\endgroup\$primo– primo2014年02月16日 02:52:23 +00:00Commented Feb 16, 2014 at 2:52 -
1\$\begingroup\$ Now that the contest is over, the best I found was 34:
$\=$_%2 .$\while$_=$_>>1||<>;print. Or, if command line options count one byte each, 27:1while$\=$_%2 .$,円$_>>=1}{using-p. \$\endgroup\$primo– primo2014年02月20日 14:22:16 +00:00Commented Feb 20, 2014 at 14:22
Javascript - (削除) 56 (削除ここまで) 48 and (削除) 36 (削除ここまで) 28 characters
- Does not works with negative numbers.
Thanks to @Blender for shaving 8 characters.
This form takes input and shows output, 48 characters:
x=prompt();for(a="";x;x=~~(x/2))a=x%2+a;alert(a)
If just an instruction that puts in a variable a the binary form of a variable x is needed (and you don't bother in destroying the x value as a side-effect), here it is with 28 characters:
for(a="";x;x=~~(x/2))a=x%2+a
-
1\$\begingroup\$ You can replace
Math.floorwith~~, as the range for the numbers is small. \$\endgroup\$Blender– Blender2014年02月16日 11:53:41 +00:00Commented Feb 16, 2014 at 11:53 -
\$\begingroup\$ @Blender Thanks, I knew that there existed some way, just could not find it. \$\endgroup\$Victor Stafusa– Victor Stafusa2014年02月16日 12:01:50 +00:00Commented Feb 16, 2014 at 12:01
-
\$\begingroup\$ @Victor I don't know javascript so I might be wrong but at the end when you say
a=x%2+acould this be shortened toa+=x%2? It works in all languages I know. \$\endgroup\$Albert Renshaw– Albert Renshaw2014年02月16日 23:10:53 +00:00Commented Feb 16, 2014 at 23:10 -
\$\begingroup\$ @AlbertRenshaw No, This would be the same as
a=a+x%2, but that+is for string concatenation. I.e, your suggestion results in the digits in the backwards order. \$\endgroup\$Victor Stafusa– Victor Stafusa2014年02月17日 01:55:54 +00:00Commented Feb 17, 2014 at 1:55
Python - (削除) 61 (削除ここまで) 60 characters
x=input();print"".join("01"[x>>i&1]for i in range(15,-1,-1))
-
2\$\begingroup\$ You can get rid of the space between
printand"". \$\endgroup\$Blender– Blender2014年02月16日 11:54:32 +00:00Commented Feb 16, 2014 at 11:54 -
\$\begingroup\$ @Blender I was just about to suggest the same thing :) \$\endgroup\$Albert Renshaw– Albert Renshaw2014年02月16日 23:13:48 +00:00Commented Feb 16, 2014 at 23:13
-
\$\begingroup\$ @Blender Ha true, didn't even notice. Done! \$\endgroup\$C0deH4cker– C0deH4cker2014年02月17日 03:54:49 +00:00Commented Feb 17, 2014 at 3:54
-
\$\begingroup\$ if you call it from the command-line you could leave aside
printas it automatically returns the result \$\endgroup\$paul.oderso– paul.oderso2016年08月18日 13:14:35 +00:00Commented Aug 18, 2016 at 13:14
C, 55 chars
Prints an extra leading zero (for the sake of 2 bytes).
Recursion within printf reverses the print order, so the algorithm extracts bits right-to-left but prints left-to-right.
EDIT: Saved a char by using putchar instead of printf.
f(x){(x*=x<0?-printf("-"):1)&&f(x/2);putchar(48+x%2);}
Excel, (削除) 91 90 81 (削除ここまで) 74
Trailing parens already discounted.
Compatibility note: CONCAT() was available only after later versions of 2016. It replaced CONCATENATE(). Tested in Excel online.
Formulae
A1- InputB1-=A1+4^8*(A1<0)(13) - Convert to 16-bit unsigned equivalent.B2-=1+INT(LOG(B1,2))(15) - Number of binary digits + 1
Code (46):
Convert B1 to binary.
=IF(A1=0,0,CONCAT(--ISODD(B1/2^(B2-SEQUENCE(B2)))))
Works in the full 16-bit range.
-
\$\begingroup\$ I would normally recommend using implicit conversion in th if statement to get something like
=IF(A1,CONCAT(--ISODD(B1/2^(B2-ROW(OFFSET(B1,,,B2))))),but as omitting trailing parens is valid you can do better with=A1+4^8*(A1<0=1+INT(LOG(B1,2=IF(A1=0,,CONCAT(--ISODD(B1/2^(B2-ROW(OFFSET(B1,,,B2which would be 82 bytes, if I am counting correctly \$\endgroup\$Taylor Raine– Taylor Raine2020年07月20日 16:18:37 +00:00Commented Jul 20, 2020 at 16:18 -
1\$\begingroup\$ Oh, right, I meant to subtract the trailing parens. Guess I forgot. \$\endgroup\$General Grievance– General Grievance2020年07月20日 16:51:27 +00:00Commented Jul 20, 2020 at 16:51
-
\$\begingroup\$ I even stacked the parens at the end for that reason! Unfortunately, with the implicit conversion, I got
FALSEas the string, so I would have had to add an extra 2 characters anyway. Otherwise, I could have just omitted the 0. \$\endgroup\$General Grievance– General Grievance2020年07月20日 17:00:08 +00:00Commented Jul 20, 2020 at 17:00
C, 81
char b[17];i=15;main(x){scanf("%d",&x);while(i+1)b[i--]=(x&1)+48,x>>=1;puts(b);}
The output has strictly 16 bits (including padding zeros)
Bash, 44
f=b+=n/2**e%2*10**e,2**e++/n?f=b:f;echo $[f]
Pass an input value to the script through the environment variable n. The decimal representation of the binary result cannot exceed LONG_MAX.
This should also be compatible with ksh93 and zsh if b and e are initialized to 0 and proper arithmetic expansion is used.
-
1\$\begingroup\$ I don't believe this is valid since it assumes that
nis already defined, making it a snippet. That could be fixed by taking input as a command-line argument and settingnto that in your script. \$\endgroup\$a spaghetto– a spaghetto2015年12月22日 20:14:49 +00:00Commented Dec 22, 2015 at 20:14 -
\$\begingroup\$ @quartata Variables in a math context in shell are implicitly zero. For the purpose of golf it makes more sense to do
n=127 sh -c '...'thansh -c 'n=1ドル ...' _ 127. There's no reason to prefer one over the other in this case as they're both perfectly typical way to pass values. \$\endgroup\$ormaaj– ormaaj2015年12月22日 22:14:31 +00:00Commented Dec 22, 2015 at 22:14
Apps Script + Google Sheets, (削除) 147 (削除ここまで) (削除) 144 (削除ここまで) 121 bytes
Script
function j(decNumb){var str='';do{str=String(decNumb%2)+str;decNumb=decNumb/2|0;}while(decNumb>=1);return parseInt(str);}
Sheet
=j(b1)
Modified version of this script by ZygD.
-
\$\begingroup\$ Can you remove any spaces? \$\endgroup\$NoOneIsHere– NoOneIsHere2016年06月16日 20:06:22 +00:00Commented Jun 16, 2016 at 20:06
Haskell, 66 bytes
c 0=0
c n=c(div n 2)*10+mod n 2
b('-':r)='-':b r
b r=show.c.read$r
Call with b "-1023", add main=interact b for a complete program or
try it on Ideon.
c performs the conversion for positive integers.
b r=show.c.read$r converts a string to a number, applies c and converts back to string.
b('-':r)='-':b r strips a possible leading - and re-appends it to the result.
PowerShell, (削除) 59 (削除ここまで) (削除) 87 (削除ここまで) (削除) 82 (削除ここまで) 70 bytes
+28 bytes for supporting negative numbers.
-12 bytes thanks to @ASCII-only
param($d)$m=$d-lt0;while($d){$n="01"[$d%2]+$n;$d=($d-$d%2)/2}'-'*$m+$n
Adapted from this code. Takes input through a commandline parameter -d.
-
\$\begingroup\$ What about numbers with sign? \$\endgroup\$mazzy– mazzy2019年03月10日 05:46:42 +00:00Commented Mar 10, 2019 at 5:46
-
\$\begingroup\$ 73? \$\endgroup\$ASCII-only– ASCII-only2019年03月11日 03:09:54 +00:00Commented Mar 11, 2019 at 3:09
-
\$\begingroup\$ so close, yet so far. also close \$\endgroup\$ASCII-only– ASCII-only2019年03月11日 03:14:37 +00:00Commented Mar 11, 2019 at 3:14
-
\$\begingroup\$ oh wait 70 \$\endgroup\$ASCII-only– ASCII-only2019年03月11日 03:22:05 +00:00Commented Mar 11, 2019 at 3:22
APL(NARS), 17 chars, 34 bytes
{2∣⌊⍵÷2*(⍺-1)..0}
It is a copy and modify of Adam answer https://codegolf.stackexchange.com/a/90107 in the way one can add the parameter for the bits lenght, and ⎕IO for this function (here is ⎕IO=1) should have no importance...
f←{2∣⌊⍵÷2*(⍺-1)..0}
16 f 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
32 f 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
32 f ̄1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
16 f ̄1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
64 f ̄12345678
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 1 1 1 0 0 1 1 1 1 0 1 0 1 1 0 0 1 0
it seeme easy handle the number of bits in this way (I cheked that last result should be right)
Smalltalk (Smalltalk/X), 63/78
the first version creates an intermediate string (78):
t:=Number readFrom:Stdin.
((15to:1by:-1)collect:[:i|0ドル+(t>>i&1)]as:String)print
actually, there is no need to create the string; just output the chars (63):
t:=Number readFrom:Stdin.
15to:1by:-1 do:[:i|(0ドル+(t>>i&1))print]
mhmh - is there a shorter way to read to a number?
Python 3.x: 65 characters
b=lambda n:n<2 and'01'[n]or b(n//2)+b(n%2);print(b(int(input())))
C# - 104
string p(int d){var r="";long i=1;while(r.Length<=64){var g=d&i;r=(g!=0)? "1"+r:"0"+r;i=i<<1;}return r;}
This method will convert decimal to binary up to 64 bits.
When executed the above method in Linqpad - rr = p(-32768); rr.Dump();
Output: 01111111111111111111111111111111111111111111111111000000000000000
-
\$\begingroup\$ The spec calls for "an input string". It looks like this method accepts an
int. \$\endgroup\$Poke– Poke2016年06月16日 20:19:00 +00:00Commented Jun 16, 2016 at 20:19
Kotlin, 82 bytes
{s:String->{var i=s.toInt()
var r=""
(0..15).map{r="${i and 1}$r"
i=i shr 1}
r}()}
Small Basic, 133 bytes
A script that inputs from and outputs to the TextWindow console.
n=TextWindow.Read()
While n>0
c=c+1
x[c]=Math.Remainder(n,2)
n=Math.Floor(n/2)
EndWhile
For i=0To c-1
TextWindow.Write(x[c-i])
EndFor
Try it at SmallBasic.com Requires Silverlight and thus must be run in IE.
I/O is taken/given from the black console.
-22 bytes thanks to @Neil
-
\$\begingroup\$ Can you not use
For i=0To c-1? \$\endgroup\$Neil– Neil2018年07月13日 20:57:42 +00:00Commented Jul 13, 2018 at 20:57 -
\$\begingroup\$ @Neil - I absolutely can. Great catch! \$\endgroup\$Taylor Raine– Taylor Raine2018年07月15日 21:01:15 +00:00Commented Jul 15, 2018 at 21:01
C (gcc), (削除) 50 (削除ここまで) 43 bytes
-7 bytes thanks to ceilingcat.
f(n){printf("-%d"+(~n?n&&f(n/2),1:0),n&1);}
-
\$\begingroup\$ @ceilingcat Cheers! \$\endgroup\$gastropner– gastropner2018年08月04日 04:48:04 +00:00Commented Aug 4, 2018 at 4:48
Perl 5, 26 bytes
Produces an additional leading 0 which can be avoided with two additional bytes (appending }{)
$\=($_&1).$\;redo if$_>>=1
Clojure 129 bytes#
Golfed version:
(defn p[n](loop[m 32768](if(> m 0)(do(if(= 0(bit-and m n))(print"0")(print"1"))(recur(unsigned-bit-shift-right m 1)))(println))))
Ungolfed:
(defn print-as-binary [n]
(loop [mask 32768]
(if (> mask 0)
(do
(if (= 0 (bit-and mask n))
(print "0")
(print "1"))
(recur (unsigned-bit-shift-right mask 1)))
(println))))
PostgreSQL, 184 bytes
WITH RECURSIVE t(a,b,c)AS(SELECT abs(1ドル),0,0 UNION ALL SELECT a/2,a%2,1from t where a>0)select case when 1ドル<0then '-'else ''end||string_agg(b::text,''order by a)from t where c>0OR 1ドル=0
137 bytes if only handling positive numbers
WITH RECURSIVE t(a,b,c)AS(SELECT 1,0,0ドル UNION ALL SELECT a/2,a%2,1from t where a>0)select string_agg(b::text,''order by a)from t where c>0
Input is given as an integer query parameter (using the extended query protocol or by wrapping in a function) and output is given as a string.
the MSB of signed variables controls if they are negative- that sounds like sign bit, however as the range-32768..32767suggests, you want 2's complement. So which do you want?.. \$\endgroup\$