Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Make my value binary

+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
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

You didn’t answer on sandbox so I guess I’ll ask here: can we output in native formats? Like python u... (2 comments)

14 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+1
−0

Haskell, 29 bytes

import Text.Printf
printf"%b"

Try it online!


Without builtins:

Haskell, 37 bytes

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

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+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!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

32 bytes version (1 comment)
+3
−0

Ahead, 20 bytes

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

Control flow goes like this:

Ahead program flow diagram

# orange path: init
I   # read number
# 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!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Vyxal, 1 byte

b

Try it Online!

b is for binary...

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Python 3, 3 bytes

bin

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

J, 2 bytes

#:

Try it online!

Antibase two

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Japt, 1 byte

¤

Try it or run all test cases

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

dc, 4 bytes

2o?p

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Julia 1.0, 9 bytes

bitstring

Try it online!

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

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

JavaScript, 16 bytes

n=>n.toString(2)

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+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
Readability: priceless

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Ruby, 11 bytes

->n{"%b"%n}

Simple string formatting.

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Sclipting, (UTF-16) 14 bytes

要감啃終丟併反

Explanation:

Push input
要     While the top number is non-zero
 감啃  Shift the top number by 1 bit and push both the bit and shifted result 
終     End loop
丟併反 Drop leading zero, concatenate, and reverse
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »