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

Dashboard
Notifications
Mark all as read
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
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)

11 answers

+3
−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?
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!

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

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.

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

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!

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!

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

0 comment threads

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

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

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!

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 »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!