### Communities

tag:snake search within a tag
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
created:<1w created < 1 week ago
post_type:xxxx type of post
Challenges

+2
−0

# Challenge

What do computers understand? That's right, binary. All files are turned into binary digits when you run them, but what if I suggest giving you an int then turn it into it's binary value.

• Take input of a non-negative decimal integer and output its binary value/form.
• Any preceding zeros don't need to be outputted.
• This is code-golf, so the shortest program wins. Yep, not by language.

# Test Cases

n = 0
Output: 0

n = 1
Output: 1

n = 5
Output: 101

n = 12
Output: 1100

n = 31
Output: 11111

n = 4096
Output: 1000000000000

Why does this post require moderator attention?
Why should this post be closed?

+4
−0

# C (gcc), 35 32 bytes

Saved 3 bytes thanks to Lundin

f(n){n&&f(n/2);putchar(n&1|48);}


This solution exploits that leading zeros, while not required, are also not forbidden by the task.

Try it online!

Why does this post require moderator attention?

32 bytes version (1 comment)
+3
−0

I>:2%r
vn:/2\<
>}KO@


Control flow goes like this:

# orange path: init
# green path: main loop
>   # go east (start of loop)
:   # dup input
2%  # take mod 2 (get lowest bit)
r<  # half-turn right, go east (enter second line)
\   # swap top two stack items (put bit under input)
2/  # divide input by 2 (right shift)
:   # dup input
n   # if input is nonzero, go north (continue loop)
# else, keep going (enter red path)
# red path: end
v>  # go south, then east (enter third line)
}   # right rotate stack (put input on bottom)
K   # stack reduce (do until <2 items)...
O   # output number
@   # end


Try it online!

Why does this post require moderator attention?

+2
−0

# Vyxal, 1 byte

b


Try it Online!

b is for binary...

Why does this post require moderator attention?

+2
−0

# jq, 48 bytes

[while(.>0;./2|floor)]|map(.%2)|reverse|join("")


Try it online!

jq is the Language of the month for September!

if the output is not required as per the question, join can be omitted.

Why does this post require moderator attention?

+1
−0

# Ruby, 11 bytes

->n{"%b"%n}


Simple string formatting.

Try it online!

Why does this post require moderator attention?

+1
−0

# Python 3, 3 bytes

bin


Try it online!

Why does this post require moderator attention?

+1
−0

# JavaScript, 16 bytes

n=>n.toString(2)


Try it online!

Why does this post require moderator attention?

+1
−0

# Japt, 1 byte

¤

Why does this post require moderator attention?

+1
−0

# Julia 1.0, 9 bytes

bitstring


Try it online!

Lots of leading zeros, but those are allowed (just not necessary)!

Why does this post require moderator attention?

+1
−0

## Embed ESCR, 16 characters

[int n "base 2"]

The INT function produces the text representation of an integer with lots of formatting options. The only non-default option needed was to set the number base to 2.

Full example:

loop with n from 0 to 10
show n ": " [int n "base 2"]
endloop

and its output:

0: 0
1: 1
2: 10
3: 11
4: 100
5: 101
6: 110
7: 111
8: 1000
9: 1001
10: 1010

16 characters: OK

Why does this post require moderator attention?

+1
−0

# dc, 4 bytes

2o?p


Try it online!

Why does this post require moderator attention?

+1
−0

import Text.Printf
printf"%b"


Try it online!

Without builtins:

f n|n<2=[n]|0<1=f(div n 2)++[rem n 2]


Try it online!

Why does this post require moderator attention?

+1
−0

# Sclipting, (UTF-16) 14 bytes

要감啃終丟併反


Explanation:

Push input

감啃  Shift the top number by 1 bit and push both the bit and shifted result


Why does this post require moderator attention?

+1
−0

# J, 2 bytes

#:


Try it online!

Antibase two

Why does this post require moderator attention?