Make my value binary
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
[C (gcc)], 35 32 bytes Save …
3y ago
[Ahead], 20 bytes ```ahead …
3y ago
[jq], 48 bytes [while(. …
3y ago
Vyxal, 1 byte ``` b ``` …
3y ago
Embed ESCR, 16 characters [ …
3y ago
[dc], 4 bytes 2o?p T …
3y ago
J, 2 bytes ```J #: ``` …
3y ago
[Julia 1.0], 9 bytes …
3y ago
[Ruby], 11 bytes ```ruby - …
3y ago
[Python 3], 3 bytes …
3y ago
[Haskell], 29 bytes …
3y ago
[Sclipting], (UTF-16) 14 bytes …
3y ago
JavaScript, 16 bytes …
3y ago
Japt, 1 byte ¤ Try i …
3y ago
14 answers
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
0 comment threads
Ahead, 20 bytes
I>:2%r
vn:/2\<
>}KO@
Control flow goes like this:
# 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
0 comment threads
jq, 48 bytes
[while(.>0;./2|floor)]|map(.%2)|reverse|join("")
jq is the Language of the month for September!
if the output is not required as per the question, join
can be omitted.
0 comment threads
Julia 1.0, 9 bytes
bitstring
Lots of leading zeros, but those are allowed (just not necessary)!
0 comment threads
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
1 comment thread