Sum of Powers of 2
$begingroup$
The Challenge
Given an integer input x
where 1 <= x <= 255
, return the results of powers of two that when summed give x
.
Examples
Given the input:
86
Your program should output:
64 16 4 2
Input:
240
Output:
128 64 32 16
Input:
1
Output:
1
Input:
64
Output:
64
The output may contain zeros if the certain power of two is not present in the sum.
For example, input 65
may output 0 64 0 0 0 0 0 1
.
Scoring
This is code-golf, so the shortest answer in each language wins.
code-golf binary
$endgroup$
|
show 7 more comments
$begingroup$
The Challenge
Given an integer input x
where 1 <= x <= 255
, return the results of powers of two that when summed give x
.
Examples
Given the input:
86
Your program should output:
64 16 4 2
Input:
240
Output:
128 64 32 16
Input:
1
Output:
1
Input:
64
Output:
64
The output may contain zeros if the certain power of two is not present in the sum.
For example, input 65
may output 0 64 0 0 0 0 0 1
.
Scoring
This is code-golf, so the shortest answer in each language wins.
code-golf binary
$endgroup$
5
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
Jan 28 at 20:56
2
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
Jan 28 at 20:58
4
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
Jan 28 at 21:38
5
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
Jan 28 at 22:28
5
$begingroup$
I think you might want to state that any power of two may only be used once. Otherwise somebody could output "1 1 1" for the input 3.
$endgroup$
– Black Owl Kai
Jan 29 at 10:58
|
show 7 more comments
$begingroup$
The Challenge
Given an integer input x
where 1 <= x <= 255
, return the results of powers of two that when summed give x
.
Examples
Given the input:
86
Your program should output:
64 16 4 2
Input:
240
Output:
128 64 32 16
Input:
1
Output:
1
Input:
64
Output:
64
The output may contain zeros if the certain power of two is not present in the sum.
For example, input 65
may output 0 64 0 0 0 0 0 1
.
Scoring
This is code-golf, so the shortest answer in each language wins.
code-golf binary
$endgroup$
The Challenge
Given an integer input x
where 1 <= x <= 255
, return the results of powers of two that when summed give x
.
Examples
Given the input:
86
Your program should output:
64 16 4 2
Input:
240
Output:
128 64 32 16
Input:
1
Output:
1
Input:
64
Output:
64
The output may contain zeros if the certain power of two is not present in the sum.
For example, input 65
may output 0 64 0 0 0 0 0 1
.
Scoring
This is code-golf, so the shortest answer in each language wins.
code-golf binary
code-golf binary
edited Jan 28 at 22:27
SpookyGengar
asked Jan 28 at 20:29
SpookyGengarSpookyGengar
842819
842819
5
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
Jan 28 at 20:56
2
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
Jan 28 at 20:58
4
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
Jan 28 at 21:38
5
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
Jan 28 at 22:28
5
$begingroup$
I think you might want to state that any power of two may only be used once. Otherwise somebody could output "1 1 1" for the input 3.
$endgroup$
– Black Owl Kai
Jan 29 at 10:58
|
show 7 more comments
5
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
Jan 28 at 20:56
2
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
Jan 28 at 20:58
4
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
Jan 28 at 21:38
5
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
Jan 28 at 22:28
5
$begingroup$
I think you might want to state that any power of two may only be used once. Otherwise somebody could output "1 1 1" for the input 3.
$endgroup$
– Black Owl Kai
Jan 29 at 10:58
5
5
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
Jan 28 at 20:56
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
Jan 28 at 20:56
2
2
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
Jan 28 at 20:58
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
Jan 28 at 20:58
4
4
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
Jan 28 at 21:38
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
Jan 28 at 21:38
5
5
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
Jan 28 at 22:28
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
Jan 28 at 22:28
5
5
$begingroup$
I think you might want to state that any power of two may only be used once. Otherwise somebody could output "1 1 1" for the input 3.
$endgroup$
– Black Owl Kai
Jan 29 at 10:58
$begingroup$
I think you might want to state that any power of two may only be used once. Otherwise somebody could output "1 1 1" for the input 3.
$endgroup$
– Black Owl Kai
Jan 29 at 10:58
|
show 7 more comments
55 Answers
55
active
oldest
votes
1 2
next
$begingroup$
JavaScript (ES6), 28 bytes
f=n=>n?[...f(n&~-n),n&-n]:
Try it online!
$endgroup$
9
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
Jan 29 at 0:05
4
$begingroup$
@sergiol, why wouldn't you normally upvote a JS solution? A good solution is a good solution regardless of the language used or who posted it.
$endgroup$
– Shaggy
Jan 29 at 13:23
$begingroup$
@Shaggy Because Arnauld seems the only person to do such Javascript solutions. His answers are pure genius!
$endgroup$
– sergiol
Jan 29 at 15:11
3
$begingroup$
@sergiol Thanks for the compliment, but that's not quite true. I'm regularly outgolfed by more clever answers -- and that's what this site is all about. ^^
$endgroup$
– Arnauld
Jan 29 at 15:18
$begingroup$
@Oliver I'm not sure. It seems like leading zeros (before 128) are forbidden. Otherwise, another possible variant isf=n=>n&&f(n&~-n)+[,n&-n]
.
$endgroup$
– Arnauld
Jan 29 at 19:47
|
show 1 more comment
$begingroup$
Pure Bash, 20
echo $[2**{7..0}&$1]
Try it online!
Explanation
{7..0} # Brace expansion: 7 6 5 4 3 2 1 0
2**{7..0} # Brace expansion: 128 64 32 16 8 4 2 1
2**{7..0}&$1 # Brace expansion: 128&n 64&n 32&n 16&n 8&n 4&n 2&n 1&n (Bitwise AND)
$[2**{7..0}&$1] # Arithmetic expansion
echo $[2**{7..0}&$1] # and output
$endgroup$
add a comment |
$begingroup$
Jelly, 4 bytes
-2 since we may output zeros in place of unused powers of 2 :)
Ḷ2*&
Try it online!
How?
Ḷ2*& - Link: integer, n e.g. 10
Ḷ - lowered range of n [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2* - two to the power of [ 1, 2, 4, 8, 16, 32, 64,128,256,512]
& - bit-wise and [ 0, 2, 0, 8, 0, 0, 0, 0, 0, 0]
$endgroup$
add a comment |
$begingroup$
Jelly, 6 bytes
BUT’2*
Try it online!
Explanation
BUT here is an explanation (note: I had assumed that we may only output the powers of 2 themselves and nothing else):
BUT’2* – Monadic link. Takes a number N as input. Example: 86
B – Convert N to binary. [1, 0, 1, 0, 1, 1, 0]
U – Reverse. [0, 1, 1, 0, 1, 0, 1]
T – Truthy indices. [2, 3, 5, 7]
’ – Decrement. [1, 2, 4, 6]
2* – Raise 2 to that power. [2, 4, 16, 64]
"Proof" that it works correctly. The standard representation of an integer $
X$ in base 2 is a list ${x_1, x_2, x_3,cdots, x_n}$, where $x_iin{0,1},:forall:: iinoverline{1,n}$, such that:
$$X=sum_{i=1}^n x_icdot 2^{n-i}$$
The indices $i$ such that $x_i=0$ obviously have no contribution so we're only interested in finding those such that $x_i=1$. Since subtracting $i$ from $n$ is not convenient (the powers of two all have exponents of the form $n-i$, where $i$ is any index of a $1$), instead of finding the truthy indices in this list we reverse it and then find them "backwards" with UT
. Now that we've found the correct indices all we have to do is raise $2$ to those powers.
$endgroup$
1
$begingroup$
"ASCII-only" Sneaky’
there...
$endgroup$
– Erik the Outgolfer
Jan 28 at 21:05
1
$begingroup$
@EriktheOutgolfer I guessBUT2*H
would work though.
$endgroup$
– Mr. Xcoder
Jan 28 at 21:08
1
$begingroup$
Pretty impressive that this works with an input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:47
add a comment |
$begingroup$
Python, 35 bytes
lambda n:[n&2**i for i in range(8)]
Little-endian with zeros at unused powers of 2.
Try it online!
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0
).
2*⍸⍢⌽⍤⊤
Try it online!
2
two*
raised to the power of⍸
the ɩndices where true⍢
while⌽
reversed⍤
of⊤
the binary representation
$endgroup$
add a comment |
$begingroup$
Sledgehammer 0.2, 3 bytes
⡔⡸⢣
Decompresses into {intLiteral[2],call[NumberExpand,2]}
.
Sledgehammer is a compressor for Wolfram Language code using Braille as a code page. The actual size of the above is 2.75 bytes, but due to current rules on meta, padding to the nearest byte is counted in code size.
$endgroup$
2
$begingroup$
Huh! Neat little language, and all the characters are actually printable.
$endgroup$
– LegionMammal978
Jan 29 at 15:41
$begingroup$
And now I can't get the Peter Gabriel song out of my mind...
$endgroup$
– Digital Trauma
Jan 31 at 22:14
add a comment |
$begingroup$
05AB1E, 3 bytes
Ýo&
Port of @JonathanAllan's Jelly answer, so make sure to upvote him!
Contains zeros (including -loads of- trailing zeros).
Try it online or verify all test cases.
Explanation:
Ý # Create a list in the range [0, (implicit) input]
# i.e. 15 → [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
# i.e. 16 → [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
o # Take 2 to the power of each value
# → [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768]
# → [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536]
& # Bitwise-AND each value with the (implicit) input
# 15 → [1,2,4,8,0,0,0,0,0,0,0,0,0,0,0,0]
# 16 → [0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0]
# (and output the result implicitly)
$endgroup$
1
$begingroup$
... what?! Never honestly seenbitwise and
used in osabie. Nice one.
$endgroup$
– Magic Octopus Urn
Jan 29 at 15:40
$begingroup$
@MagicOctopusUrn I indeed also don't use it very often. Can't even find any other answer I've used&
in. xD I have used Bitwise-XOR a couple of times, like here or here and Bitwise-NOT once here (which I later removed again after golfing further..). I do use Bitwise-AND, XOR, OR, NOT, SHIFT, etc. pretty often in Java, but in 05AB1E not so much. :)
$endgroup$
– Kevin Cruijssen
Jan 29 at 15:55
add a comment |
$begingroup$
Catholicon, 3 bytes
ṫĊŻ
Try it online!
Explanation:
ṫ Decompose into the largest values where:
Ċ the input
Ż the bit count is truthy (equal to one)
$endgroup$
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
Jan 28 at 22:25
$begingroup$
Works with 302231454903657293676544. Nice.
$endgroup$
– Michael Karas
Feb 3 at 13:54
add a comment |
$begingroup$
Wolfram Language (Mathematica), 17 bytes
#~NumberExpand~2&
Try it online!
Mathematica strikes again.
$endgroup$
$begingroup$
This also works with input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:56
add a comment |
$begingroup$
R, 27 23 bytes
bitwAnd(scan(),2^(7:0))
Try it online!
Unrolled code and explanation :
A = scan() # get input number A from stdin
# e.g. A = 65
bitwAnd( A , 2^(7:0)) # bitwise AND between all powers of 2 : 2^7 ... 2^0 and A
# and implicitly print the result
# e.g. B = bitwAnd(65, c(128,64,32,16,8,4,2,1)) = c(0,64,0,0,0,0,0,1)
- 4 bytes thanks to @Kirill L.
$endgroup$
1
$begingroup$
23 bytes with bitwise and.
$endgroup$
– Kirill L.
Jan 29 at 9:53
$begingroup$
@KirillL.: brilliant !
$endgroup$
– digEmAll
Jan 29 at 13:16
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 29 bytes
Contains 5 unprintable characters.
n=>"@ ".Select(a=>a&n)
Explanation
//Lambda taking one parameter 'n'
n=>
//String with ASCII characters 128, 64, 32, 16, 8, 4, 2, and 1
"@ "
//Iterate through all the chars of the above string and transform them to
.Select(a=>
//A bitwise AND operation between the integer value of the current char and the input value
a&n)
Try it online!
$endgroup$
$begingroup$
But we need to get rid of zeros, liken=>new int[8].Select((j,i)=>1<<i&n).Where(i=>i!=0)
The part beforeWhere
is five bytes shorter btw
$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 8:49
$begingroup$
@polfosolThe output may contain zeros
$endgroup$
– Jo King
Jan 29 at 9:06
2
$begingroup$
@JoKing Still,n=>new int[8].Select((j,i)=>1<<i&n)
is 35 bytes long and we won't need additional flags and text encodings.
$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 13:02
1
$begingroup$
Using ascii characters 0-7 should be shorter, egn=>"INSERT ASCII HERE".Select(a=>1<<a&n)
But I'm on a mobile device that can't display or type unprintables, so I'll have to wait till I get home to update the answer
$endgroup$
– Embodiment of Ignorance
Jan 29 at 16:34
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 38 bytes
x=>{for(int y=8;y-->0;Print(x&1<<y));}
Try it online!
$endgroup$
$begingroup$
aw, close :P
$endgroup$
– ASCII-only
Jan 29 at 7:45
1
$begingroup$
Fails for inputs1
,2
,4
,8
,16
, etc. (thex>y
should bex>=y
instead).
$endgroup$
– Kevin Cruijssen
Jan 29 at 9:37
1
$begingroup$
@ASCIIOnly - I'm telling you, the range operator is going to be sweet :)
$endgroup$
– dana
Jan 29 at 13:43
$begingroup$
@ASCII-only Mean while, you can use the flag/u:System.Linq.Enumerable
and try this for 31 bytes
$endgroup$
– Embodiment of Ignorance
Jan 29 at 23:18
$begingroup$
@EmbodimentofIgnorance sure. but i'd prefer not to list language as "C#/u:System.Linq.Enumerable
" :P
$endgroup$
– ASCII-only
Jan 30 at 4:54
add a comment |
$begingroup$
C (gcc), 39 bytes
f(n){for(;n;n&=n-1)printf("%d ",n&-n);}
Try it online!
$endgroup$
add a comment |
$begingroup$
05AB1E, 7 bytes
2вRƶ<oò
explanation:
2в convert input to binary array
R reverse array
ƶ< multiply each item by it's index and subtract 1
oò 2^item then round down
Try it online!
$endgroup$
$begingroup$
Also works with input of 302231454903657293676544
$endgroup$
– Michael Karas
Feb 3 at 14:00
add a comment |
$begingroup$
Haskell, 29 bytes
(mapM(n->[0,2^n])[7,6..0]!!)
Try it online!
$endgroup$
add a comment |
$begingroup$
C (clang), 133 110 63 58 bytes
58-byte solution thanks to @ceilingcat.
x=256;main(y){for(scanf("%d",&y);x/=2;)printf("%d ",y&x);}
Try it online!
$endgroup$
$begingroup$
In C89, you can declare likemain(){}
and the return type defaults to int. Same for variables at global scope. Also, at least on normal implementations like clang, printf and scanf work without prototypes. You get warnings of course, but it's still valid C89 (maybe) or at least K&R C for them to be implicitly declared. The types of the C objects you pass as args defines how they're passed, so achar*
andint*
will Just Work without truncating pointers to 32-bit on x86-64 or anything. (Default argument promotions happen, same as for variadic functions which they are anyway.)
$endgroup$
– Peter Cordes
Jan 30 at 3:46
$begingroup$
Or is this aiming to be valid C11 with no undefined behaviour? If so, proudly proclaim it. :) And BTW, writing a function that takes an output array as an arg would probably be smaller. Anyway, see Tips for golfing in C
$endgroup$
– Peter Cordes
Jan 30 at 3:48
$begingroup$
You can use bitwise&
to check if a bit is set. Likey&(1<<x)&&printf("%d ",1<<x);
. Or to not skip zeros, justprintf("%d ", y&(1<<x))
. Or instead of counting bit positions, usex=256
andx>>=1
to shift the mask.main(y){int x=256;for(scanf("%d",&y);x>>=1;)printf("%d ",y&x);}
63 bytes Try it online! clang will even compile that with-std=c11
$endgroup$
– Peter Cordes
Jan 30 at 4:11
$begingroup$
44 bytes
$endgroup$
– ceilingcat
Feb 9 at 1:25
add a comment |
$begingroup$
MATL, 5 bytes
BPfqW
Try it online!
Explanation
Consider input 86
as an example.
B % Implicit input. Convert to binary (highest to lowest digits)
% STACK: [1 0 1 0 1 1 0]
P % Flip
% STACK: [0 1 1 0 1 0 1]
f % Find: indices of nonzeros (1-based)
% STACK: [2 3 5 7]
q % Subtract 1, element-wise
% STACK: [1 2 4 6]
W % Exponential with base 2, element-wise. Implicit display
% STACK: [2 4 16 64]
$endgroup$
add a comment |
$begingroup$
Perl 6, 16 12 bytes
-4 bytes thanks to Jonathan Allan
*+&2**all ^8
Try it online!
Returns an All Junction with 8 elements. This is a rather non-standard way of returning, but generally, Junctions can act as ordered (at least until autothreading is implemented) lists and it is possible to extract the values from one.
Explanation:
*+& # Bitwise AND the input with
2** # 2 raised to the power of
all ^8 # All of the range 0 to 7
$endgroup$
add a comment |
$begingroup$
Japt, 8 5 bytes
Æ&2pX
Try it
Æ&2pX :Implicit input of integer U
Æ :Map each X in the range [0,U)
& : Bitwise AND of U with
2pX : 2 to the power of X
Alternative
Suggested by Oliver to avoid the 0
s in the output using the -mf
flag.
N&2pU
Try it
N&2pU :Implicitly map each U in the range [0,input)
N :The (singleton) array of inputs
& :Bitwise AND with
2pX :2 to the power of U
:Implicitly filter and output
$endgroup$
1
$begingroup$
Nice one. You can doN&2pU
with-mf
to avoid the0
s
$endgroup$
– Oliver
Jan 29 at 2:54
add a comment |
$begingroup$
05AB1E, 9 bytes
Ýoʒ›}æʒOQ
Try it online!
This is also correct for 6-bytes, but it doesn't complete in time on TIO for 86:
05AB1E, 6 bytes
ÝoæʒOQ
Try it online!
$endgroup$
1
$begingroup$
Both your answers output an empty set for15
, instead of[1,2,4,8]
$endgroup$
– Kevin Cruijssen
Jan 29 at 7:14
1
$begingroup$
@KevinCruijssen needed2**0
, nice catch.Ý
overL
.
$endgroup$
– Magic Octopus Urn
Jan 29 at 15:29
1
$begingroup$
Ah, I know the feeling. Also hadL
instead ofÝ
at first in my answer.
$endgroup$
– Kevin Cruijssen
Jan 29 at 15:31
add a comment |
$begingroup$
Julia 0.6, 13 bytes
n->n&2.^(0:7)
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 25 bytes
->n{8.times{|i|p n&2**i}}
Try it online!
$endgroup$
add a comment |
$begingroup$
CJam, 12 bytes
{:T{2#T&}%}
Try it online!
$endgroup$
add a comment |
$begingroup$
K (oK), 19 16 bytes
-3 bytes thanks to ngn!
{*/x#2}'&|(8#2)
Try it online!
oK does not have power
operator, that's why I need a helper function {*/x#2}
(copy 2 x
times and reduce the resulting list by multiplication)
$endgroup$
$begingroup$
you can omit the{
x}
$endgroup$
– ngn
Jan 31 at 12:49
$begingroup$
@ngn Thanks! I tried it and it worked, but I wasn't sure it is acceptible.
$endgroup$
– Galen Ivanov
Jan 31 at 15:25
add a comment |
$begingroup$
TSQL, 43 39 bytes
Can't find a shorter fancy solution, so here is a standard loop.
-4 bytes thanks to MickyT and KirillL
DECLARE @y int=255
,@ int=128s:PRINT @y&@ SET @/=2IF @>0GOTO s
Try it out
When using the query approach, it would take 44 bytes:
use master
DECLARE @ int=87
SELECT @&number
FROM spt_values
WHERE'a'=type
$endgroup$
$begingroup$
using the bitwise and (&) you can save a few with the following,@ int=128s:print @y&@ set @/=2IF @>0GOTO s
. This is hinted by @KirillL for the R answer
$endgroup$
– MickyT
Jan 31 at 19:49
$begingroup$
@MickyT that worked like a charm. Thanks a lot
$endgroup$
– t-clausen.dk
Feb 1 at 0:02
add a comment |
$begingroup$
Alchemist, 125 bytes
_->In_x+128a+m
m+x+a->m+b
m+0x+a->n+a
m+0a->o+Out_b+Out_" "
n+b->n+x+c
n+0b+a->n+c
n+0a->p
o+b->o+c
o+0b->p
p+2c->p+a
p+0c->m
Try it online! or Test every input!
Explanation
_->In_x+128a+m # Initialize universe with input, 128a (value to compare to) and m (state)
m+x+a->m+b # If c has been halved, subtract min(a, x) from a and x and put its value into b
m+0x+a->n+a # If x < a, continue to state n
m+0a->o+Out_b+Out_" " # Else print and continue to state o
n+b->n+x+c # Add min(a, x) (i.e. x) back to x, and add it to c (we're collecting a back into c)
n+0b+a->n+c # Then, add the rest of a to c
n+0a->p # Then, go to state p
o+b->o+c # Add min(a, x) (i.e. a) to c - x _is_ greater than a and so contains it in its binary representation, so we're not adding back to x
o+0b->p # Then, go to state p
p+2c->p+a # Halve c into a
p+0c->m # Then go to state m
$endgroup$
add a comment |
$begingroup$
Python 2, 43 40 bytes
f=lambda n,p=1:n/p*[1]and f(n,p*2)+[p&n]
Try it online!
$endgroup$
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
Jan 28 at 22:20
1
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
Jan 28 at 22:35
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 33 bytes
n=>{for(;n>0;n&=n-1)Print(n&-n);}
Port of @Arnauld's JavaScript (ES6) answer, so make sure to upvote him!
Try it online.
Explanation:
n=>{ // Method with integer parameter and no return-type
for(;n>0 // Continue looping as long as `n` is larger than 0:
; // After every iteration:
n&=n-1) // Bitwise-AND `n` by `n-1`
Print( // Print with trailing newline:
n&-n);} // `n` bitwise-AND `-n`
$endgroup$
add a comment |
$begingroup$
Java 8, 46 bytes
n->{for(;n>0;n&=n-1)System.out.println(n&-n);}
Try it online.
Port of @Arnauld's JavaScript (ES6) answer, so make sure to upvote him!
Explanation:
n->{ // Method with integer parameter and no return-type
for(;n>0 // Continue looping as long as `n` is larger than 0:
; // After every iteration:
n&=n-1) // Bitwise-AND `n` by `n-1`
System.out.println( // Print with trailing newline:
n&-n);} // `n` bitwise-AND `-n`
$endgroup$
$begingroup$
Breakthrough in Java: 36 chars, by using this rule. Let's go back 5 years and golf all Java answers ever produced!
$endgroup$
– Olivier Grégoire
Jan 29 at 16:29
$begingroup$
@OlivierGrégoire Hmm, although I agree that it's most likely allowed, I'll pass tbh. I have used that rule when the input is for example an array, and we modify the input-array instead of returning a new one to save bytes. But adding an additional empty List argument just to store the output in to save byte with.add
instead ofSystem.out.println
seems a bit cheaty imho.
$endgroup$
– Kevin Cruijssen
Jan 29 at 17:13
$begingroup$
Kind of like howArray.sort()
doesn't actually return anything, it just modifies the input. That seems legit. In C# you can usevoid f(out int x)
orvoid f(ref int x)
to return the result, but that doesn't seem to help w/ golf in my experiments.
$endgroup$
– dana
Feb 1 at 9:17
add a comment |
1 2
next
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179174%2fsum-of-powers-of-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
55 Answers
55
active
oldest
votes
55 Answers
55
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 2
next
$begingroup$
JavaScript (ES6), 28 bytes
f=n=>n?[...f(n&~-n),n&-n]:
Try it online!
$endgroup$
9
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
Jan 29 at 0:05
4
$begingroup$
@sergiol, why wouldn't you normally upvote a JS solution? A good solution is a good solution regardless of the language used or who posted it.
$endgroup$
– Shaggy
Jan 29 at 13:23
$begingroup$
@Shaggy Because Arnauld seems the only person to do such Javascript solutions. His answers are pure genius!
$endgroup$
– sergiol
Jan 29 at 15:11
3
$begingroup$
@sergiol Thanks for the compliment, but that's not quite true. I'm regularly outgolfed by more clever answers -- and that's what this site is all about. ^^
$endgroup$
– Arnauld
Jan 29 at 15:18
$begingroup$
@Oliver I'm not sure. It seems like leading zeros (before 128) are forbidden. Otherwise, another possible variant isf=n=>n&&f(n&~-n)+[,n&-n]
.
$endgroup$
– Arnauld
Jan 29 at 19:47
|
show 1 more comment
$begingroup$
JavaScript (ES6), 28 bytes
f=n=>n?[...f(n&~-n),n&-n]:
Try it online!
$endgroup$
9
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
Jan 29 at 0:05
4
$begingroup$
@sergiol, why wouldn't you normally upvote a JS solution? A good solution is a good solution regardless of the language used or who posted it.
$endgroup$
– Shaggy
Jan 29 at 13:23
$begingroup$
@Shaggy Because Arnauld seems the only person to do such Javascript solutions. His answers are pure genius!
$endgroup$
– sergiol
Jan 29 at 15:11
3
$begingroup$
@sergiol Thanks for the compliment, but that's not quite true. I'm regularly outgolfed by more clever answers -- and that's what this site is all about. ^^
$endgroup$
– Arnauld
Jan 29 at 15:18
$begingroup$
@Oliver I'm not sure. It seems like leading zeros (before 128) are forbidden. Otherwise, another possible variant isf=n=>n&&f(n&~-n)+[,n&-n]
.
$endgroup$
– Arnauld
Jan 29 at 19:47
|
show 1 more comment
$begingroup$
JavaScript (ES6), 28 bytes
f=n=>n?[...f(n&~-n),n&-n]:
Try it online!
$endgroup$
JavaScript (ES6), 28 bytes
f=n=>n?[...f(n&~-n),n&-n]:
Try it online!
edited Jan 28 at 22:12
answered Jan 28 at 20:59
ArnauldArnauld
78.1k795326
78.1k795326
9
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
Jan 29 at 0:05
4
$begingroup$
@sergiol, why wouldn't you normally upvote a JS solution? A good solution is a good solution regardless of the language used or who posted it.
$endgroup$
– Shaggy
Jan 29 at 13:23
$begingroup$
@Shaggy Because Arnauld seems the only person to do such Javascript solutions. His answers are pure genius!
$endgroup$
– sergiol
Jan 29 at 15:11
3
$begingroup$
@sergiol Thanks for the compliment, but that's not quite true. I'm regularly outgolfed by more clever answers -- and that's what this site is all about. ^^
$endgroup$
– Arnauld
Jan 29 at 15:18
$begingroup$
@Oliver I'm not sure. It seems like leading zeros (before 128) are forbidden. Otherwise, another possible variant isf=n=>n&&f(n&~-n)+[,n&-n]
.
$endgroup$
– Arnauld
Jan 29 at 19:47
|
show 1 more comment
9
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
Jan 29 at 0:05
4
$begingroup$
@sergiol, why wouldn't you normally upvote a JS solution? A good solution is a good solution regardless of the language used or who posted it.
$endgroup$
– Shaggy
Jan 29 at 13:23
$begingroup$
@Shaggy Because Arnauld seems the only person to do such Javascript solutions. His answers are pure genius!
$endgroup$
– sergiol
Jan 29 at 15:11
3
$begingroup$
@sergiol Thanks for the compliment, but that's not quite true. I'm regularly outgolfed by more clever answers -- and that's what this site is all about. ^^
$endgroup$
– Arnauld
Jan 29 at 15:18
$begingroup$
@Oliver I'm not sure. It seems like leading zeros (before 128) are forbidden. Otherwise, another possible variant isf=n=>n&&f(n&~-n)+[,n&-n]
.
$endgroup$
– Arnauld
Jan 29 at 19:47
9
9
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
Jan 29 at 0:05
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
Jan 29 at 0:05
4
4
$begingroup$
@sergiol, why wouldn't you normally upvote a JS solution? A good solution is a good solution regardless of the language used or who posted it.
$endgroup$
– Shaggy
Jan 29 at 13:23
$begingroup$
@sergiol, why wouldn't you normally upvote a JS solution? A good solution is a good solution regardless of the language used or who posted it.
$endgroup$
– Shaggy
Jan 29 at 13:23
$begingroup$
@Shaggy Because Arnauld seems the only person to do such Javascript solutions. His answers are pure genius!
$endgroup$
– sergiol
Jan 29 at 15:11
$begingroup$
@Shaggy Because Arnauld seems the only person to do such Javascript solutions. His answers are pure genius!
$endgroup$
– sergiol
Jan 29 at 15:11
3
3
$begingroup$
@sergiol Thanks for the compliment, but that's not quite true. I'm regularly outgolfed by more clever answers -- and that's what this site is all about. ^^
$endgroup$
– Arnauld
Jan 29 at 15:18
$begingroup$
@sergiol Thanks for the compliment, but that's not quite true. I'm regularly outgolfed by more clever answers -- and that's what this site is all about. ^^
$endgroup$
– Arnauld
Jan 29 at 15:18
$begingroup$
@Oliver I'm not sure. It seems like leading zeros (before 128) are forbidden. Otherwise, another possible variant is
f=n=>n&&f(n&~-n)+[,n&-n]
.$endgroup$
– Arnauld
Jan 29 at 19:47
$begingroup$
@Oliver I'm not sure. It seems like leading zeros (before 128) are forbidden. Otherwise, another possible variant is
f=n=>n&&f(n&~-n)+[,n&-n]
.$endgroup$
– Arnauld
Jan 29 at 19:47
|
show 1 more comment
$begingroup$
Pure Bash, 20
echo $[2**{7..0}&$1]
Try it online!
Explanation
{7..0} # Brace expansion: 7 6 5 4 3 2 1 0
2**{7..0} # Brace expansion: 128 64 32 16 8 4 2 1
2**{7..0}&$1 # Brace expansion: 128&n 64&n 32&n 16&n 8&n 4&n 2&n 1&n (Bitwise AND)
$[2**{7..0}&$1] # Arithmetic expansion
echo $[2**{7..0}&$1] # and output
$endgroup$
add a comment |
$begingroup$
Pure Bash, 20
echo $[2**{7..0}&$1]
Try it online!
Explanation
{7..0} # Brace expansion: 7 6 5 4 3 2 1 0
2**{7..0} # Brace expansion: 128 64 32 16 8 4 2 1
2**{7..0}&$1 # Brace expansion: 128&n 64&n 32&n 16&n 8&n 4&n 2&n 1&n (Bitwise AND)
$[2**{7..0}&$1] # Arithmetic expansion
echo $[2**{7..0}&$1] # and output
$endgroup$
add a comment |
$begingroup$
Pure Bash, 20
echo $[2**{7..0}&$1]
Try it online!
Explanation
{7..0} # Brace expansion: 7 6 5 4 3 2 1 0
2**{7..0} # Brace expansion: 128 64 32 16 8 4 2 1
2**{7..0}&$1 # Brace expansion: 128&n 64&n 32&n 16&n 8&n 4&n 2&n 1&n (Bitwise AND)
$[2**{7..0}&$1] # Arithmetic expansion
echo $[2**{7..0}&$1] # and output
$endgroup$
Pure Bash, 20
echo $[2**{7..0}&$1]
Try it online!
Explanation
{7..0} # Brace expansion: 7 6 5 4 3 2 1 0
2**{7..0} # Brace expansion: 128 64 32 16 8 4 2 1
2**{7..0}&$1 # Brace expansion: 128&n 64&n 32&n 16&n 8&n 4&n 2&n 1&n (Bitwise AND)
$[2**{7..0}&$1] # Arithmetic expansion
echo $[2**{7..0}&$1] # and output
edited Jan 29 at 19:12
answered Jan 28 at 23:15
Digital TraumaDigital Trauma
59.3k787225
59.3k787225
add a comment |
add a comment |
$begingroup$
Jelly, 4 bytes
-2 since we may output zeros in place of unused powers of 2 :)
Ḷ2*&
Try it online!
How?
Ḷ2*& - Link: integer, n e.g. 10
Ḷ - lowered range of n [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2* - two to the power of [ 1, 2, 4, 8, 16, 32, 64,128,256,512]
& - bit-wise and [ 0, 2, 0, 8, 0, 0, 0, 0, 0, 0]
$endgroup$
add a comment |
$begingroup$
Jelly, 4 bytes
-2 since we may output zeros in place of unused powers of 2 :)
Ḷ2*&
Try it online!
How?
Ḷ2*& - Link: integer, n e.g. 10
Ḷ - lowered range of n [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2* - two to the power of [ 1, 2, 4, 8, 16, 32, 64,128,256,512]
& - bit-wise and [ 0, 2, 0, 8, 0, 0, 0, 0, 0, 0]
$endgroup$
add a comment |
$begingroup$
Jelly, 4 bytes
-2 since we may output zeros in place of unused powers of 2 :)
Ḷ2*&
Try it online!
How?
Ḷ2*& - Link: integer, n e.g. 10
Ḷ - lowered range of n [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2* - two to the power of [ 1, 2, 4, 8, 16, 32, 64,128,256,512]
& - bit-wise and [ 0, 2, 0, 8, 0, 0, 0, 0, 0, 0]
$endgroup$
Jelly, 4 bytes
-2 since we may output zeros in place of unused powers of 2 :)
Ḷ2*&
Try it online!
How?
Ḷ2*& - Link: integer, n e.g. 10
Ḷ - lowered range of n [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2* - two to the power of [ 1, 2, 4, 8, 16, 32, 64,128,256,512]
& - bit-wise and [ 0, 2, 0, 8, 0, 0, 0, 0, 0, 0]
edited Feb 2 at 1:11
answered Jan 28 at 21:00
Jonathan AllanJonathan Allan
52.6k535170
52.6k535170
add a comment |
add a comment |
$begingroup$
Jelly, 6 bytes
BUT’2*
Try it online!
Explanation
BUT here is an explanation (note: I had assumed that we may only output the powers of 2 themselves and nothing else):
BUT’2* – Monadic link. Takes a number N as input. Example: 86
B – Convert N to binary. [1, 0, 1, 0, 1, 1, 0]
U – Reverse. [0, 1, 1, 0, 1, 0, 1]
T – Truthy indices. [2, 3, 5, 7]
’ – Decrement. [1, 2, 4, 6]
2* – Raise 2 to that power. [2, 4, 16, 64]
"Proof" that it works correctly. The standard representation of an integer $
X$ in base 2 is a list ${x_1, x_2, x_3,cdots, x_n}$, where $x_iin{0,1},:forall:: iinoverline{1,n}$, such that:
$$X=sum_{i=1}^n x_icdot 2^{n-i}$$
The indices $i$ such that $x_i=0$ obviously have no contribution so we're only interested in finding those such that $x_i=1$. Since subtracting $i$ from $n$ is not convenient (the powers of two all have exponents of the form $n-i$, where $i$ is any index of a $1$), instead of finding the truthy indices in this list we reverse it and then find them "backwards" with UT
. Now that we've found the correct indices all we have to do is raise $2$ to those powers.
$endgroup$
1
$begingroup$
"ASCII-only" Sneaky’
there...
$endgroup$
– Erik the Outgolfer
Jan 28 at 21:05
1
$begingroup$
@EriktheOutgolfer I guessBUT2*H
would work though.
$endgroup$
– Mr. Xcoder
Jan 28 at 21:08
1
$begingroup$
Pretty impressive that this works with an input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:47
add a comment |
$begingroup$
Jelly, 6 bytes
BUT’2*
Try it online!
Explanation
BUT here is an explanation (note: I had assumed that we may only output the powers of 2 themselves and nothing else):
BUT’2* – Monadic link. Takes a number N as input. Example: 86
B – Convert N to binary. [1, 0, 1, 0, 1, 1, 0]
U – Reverse. [0, 1, 1, 0, 1, 0, 1]
T – Truthy indices. [2, 3, 5, 7]
’ – Decrement. [1, 2, 4, 6]
2* – Raise 2 to that power. [2, 4, 16, 64]
"Proof" that it works correctly. The standard representation of an integer $
X$ in base 2 is a list ${x_1, x_2, x_3,cdots, x_n}$, where $x_iin{0,1},:forall:: iinoverline{1,n}$, such that:
$$X=sum_{i=1}^n x_icdot 2^{n-i}$$
The indices $i$ such that $x_i=0$ obviously have no contribution so we're only interested in finding those such that $x_i=1$. Since subtracting $i$ from $n$ is not convenient (the powers of two all have exponents of the form $n-i$, where $i$ is any index of a $1$), instead of finding the truthy indices in this list we reverse it and then find them "backwards" with UT
. Now that we've found the correct indices all we have to do is raise $2$ to those powers.
$endgroup$
1
$begingroup$
"ASCII-only" Sneaky’
there...
$endgroup$
– Erik the Outgolfer
Jan 28 at 21:05
1
$begingroup$
@EriktheOutgolfer I guessBUT2*H
would work though.
$endgroup$
– Mr. Xcoder
Jan 28 at 21:08
1
$begingroup$
Pretty impressive that this works with an input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:47
add a comment |
$begingroup$
Jelly, 6 bytes
BUT’2*
Try it online!
Explanation
BUT here is an explanation (note: I had assumed that we may only output the powers of 2 themselves and nothing else):
BUT’2* – Monadic link. Takes a number N as input. Example: 86
B – Convert N to binary. [1, 0, 1, 0, 1, 1, 0]
U – Reverse. [0, 1, 1, 0, 1, 0, 1]
T – Truthy indices. [2, 3, 5, 7]
’ – Decrement. [1, 2, 4, 6]
2* – Raise 2 to that power. [2, 4, 16, 64]
"Proof" that it works correctly. The standard representation of an integer $
X$ in base 2 is a list ${x_1, x_2, x_3,cdots, x_n}$, where $x_iin{0,1},:forall:: iinoverline{1,n}$, such that:
$$X=sum_{i=1}^n x_icdot 2^{n-i}$$
The indices $i$ such that $x_i=0$ obviously have no contribution so we're only interested in finding those such that $x_i=1$. Since subtracting $i$ from $n$ is not convenient (the powers of two all have exponents of the form $n-i$, where $i$ is any index of a $1$), instead of finding the truthy indices in this list we reverse it and then find them "backwards" with UT
. Now that we've found the correct indices all we have to do is raise $2$ to those powers.
$endgroup$
Jelly, 6 bytes
BUT’2*
Try it online!
Explanation
BUT here is an explanation (note: I had assumed that we may only output the powers of 2 themselves and nothing else):
BUT’2* – Monadic link. Takes a number N as input. Example: 86
B – Convert N to binary. [1, 0, 1, 0, 1, 1, 0]
U – Reverse. [0, 1, 1, 0, 1, 0, 1]
T – Truthy indices. [2, 3, 5, 7]
’ – Decrement. [1, 2, 4, 6]
2* – Raise 2 to that power. [2, 4, 16, 64]
"Proof" that it works correctly. The standard representation of an integer $
X$ in base 2 is a list ${x_1, x_2, x_3,cdots, x_n}$, where $x_iin{0,1},:forall:: iinoverline{1,n}$, such that:
$$X=sum_{i=1}^n x_icdot 2^{n-i}$$
The indices $i$ such that $x_i=0$ obviously have no contribution so we're only interested in finding those such that $x_i=1$. Since subtracting $i$ from $n$ is not convenient (the powers of two all have exponents of the form $n-i$, where $i$ is any index of a $1$), instead of finding the truthy indices in this list we reverse it and then find them "backwards" with UT
. Now that we've found the correct indices all we have to do is raise $2$ to those powers.
edited Jan 28 at 22:33
answered Jan 28 at 20:38
Mr. XcoderMr. Xcoder
32.1k759199
32.1k759199
1
$begingroup$
"ASCII-only" Sneaky’
there...
$endgroup$
– Erik the Outgolfer
Jan 28 at 21:05
1
$begingroup$
@EriktheOutgolfer I guessBUT2*H
would work though.
$endgroup$
– Mr. Xcoder
Jan 28 at 21:08
1
$begingroup$
Pretty impressive that this works with an input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:47
add a comment |
1
$begingroup$
"ASCII-only" Sneaky’
there...
$endgroup$
– Erik the Outgolfer
Jan 28 at 21:05
1
$begingroup$
@EriktheOutgolfer I guessBUT2*H
would work though.
$endgroup$
– Mr. Xcoder
Jan 28 at 21:08
1
$begingroup$
Pretty impressive that this works with an input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:47
1
1
$begingroup$
"ASCII-only" Sneaky
’
there...$endgroup$
– Erik the Outgolfer
Jan 28 at 21:05
$begingroup$
"ASCII-only" Sneaky
’
there...$endgroup$
– Erik the Outgolfer
Jan 28 at 21:05
1
1
$begingroup$
@EriktheOutgolfer I guess
BUT2*H
would work though.$endgroup$
– Mr. Xcoder
Jan 28 at 21:08
$begingroup$
@EriktheOutgolfer I guess
BUT2*H
would work though.$endgroup$
– Mr. Xcoder
Jan 28 at 21:08
1
1
$begingroup$
Pretty impressive that this works with an input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:47
$begingroup$
Pretty impressive that this works with an input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:47
add a comment |
$begingroup$
Python, 35 bytes
lambda n:[n&2**i for i in range(8)]
Little-endian with zeros at unused powers of 2.
Try it online!
$endgroup$
add a comment |
$begingroup$
Python, 35 bytes
lambda n:[n&2**i for i in range(8)]
Little-endian with zeros at unused powers of 2.
Try it online!
$endgroup$
add a comment |
$begingroup$
Python, 35 bytes
lambda n:[n&2**i for i in range(8)]
Little-endian with zeros at unused powers of 2.
Try it online!
$endgroup$
Python, 35 bytes
lambda n:[n&2**i for i in range(8)]
Little-endian with zeros at unused powers of 2.
Try it online!
edited Jan 28 at 22:39
answered Jan 28 at 21:09
Jonathan AllanJonathan Allan
52.6k535170
52.6k535170
add a comment |
add a comment |
$begingroup$
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0
).
2*⍸⍢⌽⍤⊤
Try it online!
2
two*
raised to the power of⍸
the ɩndices where true⍢
while⌽
reversed⍤
of⊤
the binary representation
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0
).
2*⍸⍢⌽⍤⊤
Try it online!
2
two*
raised to the power of⍸
the ɩndices where true⍢
while⌽
reversed⍤
of⊤
the binary representation
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0
).
2*⍸⍢⌽⍤⊤
Try it online!
2
two*
raised to the power of⍸
the ɩndices where true⍢
while⌽
reversed⍤
of⊤
the binary representation
$endgroup$
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0
).
2*⍸⍢⌽⍤⊤
Try it online!
2
two*
raised to the power of⍸
the ɩndices where true⍢
while⌽
reversed⍤
of⊤
the binary representation
answered Jan 28 at 21:05
AdámAdám
28.8k276204
28.8k276204
add a comment |
add a comment |
$begingroup$
Sledgehammer 0.2, 3 bytes
⡔⡸⢣
Decompresses into {intLiteral[2],call[NumberExpand,2]}
.
Sledgehammer is a compressor for Wolfram Language code using Braille as a code page. The actual size of the above is 2.75 bytes, but due to current rules on meta, padding to the nearest byte is counted in code size.
$endgroup$
2
$begingroup$
Huh! Neat little language, and all the characters are actually printable.
$endgroup$
– LegionMammal978
Jan 29 at 15:41
$begingroup$
And now I can't get the Peter Gabriel song out of my mind...
$endgroup$
– Digital Trauma
Jan 31 at 22:14
add a comment |
$begingroup$
Sledgehammer 0.2, 3 bytes
⡔⡸⢣
Decompresses into {intLiteral[2],call[NumberExpand,2]}
.
Sledgehammer is a compressor for Wolfram Language code using Braille as a code page. The actual size of the above is 2.75 bytes, but due to current rules on meta, padding to the nearest byte is counted in code size.
$endgroup$
2
$begingroup$
Huh! Neat little language, and all the characters are actually printable.
$endgroup$
– LegionMammal978
Jan 29 at 15:41
$begingroup$
And now I can't get the Peter Gabriel song out of my mind...
$endgroup$
– Digital Trauma
Jan 31 at 22:14
add a comment |
$begingroup$
Sledgehammer 0.2, 3 bytes
⡔⡸⢣
Decompresses into {intLiteral[2],call[NumberExpand,2]}
.
Sledgehammer is a compressor for Wolfram Language code using Braille as a code page. The actual size of the above is 2.75 bytes, but due to current rules on meta, padding to the nearest byte is counted in code size.
$endgroup$
Sledgehammer 0.2, 3 bytes
⡔⡸⢣
Decompresses into {intLiteral[2],call[NumberExpand,2]}
.
Sledgehammer is a compressor for Wolfram Language code using Braille as a code page. The actual size of the above is 2.75 bytes, but due to current rules on meta, padding to the nearest byte is counted in code size.
answered Jan 28 at 23:23
lirtosiastlirtosiast
18.2k438109
18.2k438109
2
$begingroup$
Huh! Neat little language, and all the characters are actually printable.
$endgroup$
– LegionMammal978
Jan 29 at 15:41
$begingroup$
And now I can't get the Peter Gabriel song out of my mind...
$endgroup$
– Digital Trauma
Jan 31 at 22:14
add a comment |
2
$begingroup$
Huh! Neat little language, and all the characters are actually printable.
$endgroup$
– LegionMammal978
Jan 29 at 15:41
$begingroup$
And now I can't get the Peter Gabriel song out of my mind...
$endgroup$
– Digital Trauma
Jan 31 at 22:14
2
2
$begingroup$
Huh! Neat little language, and all the characters are actually printable.
$endgroup$
– LegionMammal978
Jan 29 at 15:41
$begingroup$
Huh! Neat little language, and all the characters are actually printable.
$endgroup$
– LegionMammal978
Jan 29 at 15:41
$begingroup$
And now I can't get the Peter Gabriel song out of my mind...
$endgroup$
– Digital Trauma
Jan 31 at 22:14
$begingroup$
And now I can't get the Peter Gabriel song out of my mind...
$endgroup$
– Digital Trauma
Jan 31 at 22:14
add a comment |
$begingroup$
05AB1E, 3 bytes
Ýo&
Port of @JonathanAllan's Jelly answer, so make sure to upvote him!
Contains zeros (including -loads of- trailing zeros).
Try it online or verify all test cases.
Explanation:
Ý # Create a list in the range [0, (implicit) input]
# i.e. 15 → [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
# i.e. 16 → [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
o # Take 2 to the power of each value
# → [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768]
# → [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536]
& # Bitwise-AND each value with the (implicit) input
# 15 → [1,2,4,8,0,0,0,0,0,0,0,0,0,0,0,0]
# 16 → [0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0]
# (and output the result implicitly)
$endgroup$
1
$begingroup$
... what?! Never honestly seenbitwise and
used in osabie. Nice one.
$endgroup$
– Magic Octopus Urn
Jan 29 at 15:40
$begingroup$
@MagicOctopusUrn I indeed also don't use it very often. Can't even find any other answer I've used&
in. xD I have used Bitwise-XOR a couple of times, like here or here and Bitwise-NOT once here (which I later removed again after golfing further..). I do use Bitwise-AND, XOR, OR, NOT, SHIFT, etc. pretty often in Java, but in 05AB1E not so much. :)
$endgroup$
– Kevin Cruijssen
Jan 29 at 15:55
add a comment |
$begingroup$
05AB1E, 3 bytes
Ýo&
Port of @JonathanAllan's Jelly answer, so make sure to upvote him!
Contains zeros (including -loads of- trailing zeros).
Try it online or verify all test cases.
Explanation:
Ý # Create a list in the range [0, (implicit) input]
# i.e. 15 → [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
# i.e. 16 → [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
o # Take 2 to the power of each value
# → [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768]
# → [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536]
& # Bitwise-AND each value with the (implicit) input
# 15 → [1,2,4,8,0,0,0,0,0,0,0,0,0,0,0,0]
# 16 → [0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0]
# (and output the result implicitly)
$endgroup$
1
$begingroup$
... what?! Never honestly seenbitwise and
used in osabie. Nice one.
$endgroup$
– Magic Octopus Urn
Jan 29 at 15:40
$begingroup$
@MagicOctopusUrn I indeed also don't use it very often. Can't even find any other answer I've used&
in. xD I have used Bitwise-XOR a couple of times, like here or here and Bitwise-NOT once here (which I later removed again after golfing further..). I do use Bitwise-AND, XOR, OR, NOT, SHIFT, etc. pretty often in Java, but in 05AB1E not so much. :)
$endgroup$
– Kevin Cruijssen
Jan 29 at 15:55
add a comment |
$begingroup$
05AB1E, 3 bytes
Ýo&
Port of @JonathanAllan's Jelly answer, so make sure to upvote him!
Contains zeros (including -loads of- trailing zeros).
Try it online or verify all test cases.
Explanation:
Ý # Create a list in the range [0, (implicit) input]
# i.e. 15 → [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
# i.e. 16 → [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
o # Take 2 to the power of each value
# → [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768]
# → [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536]
& # Bitwise-AND each value with the (implicit) input
# 15 → [1,2,4,8,0,0,0,0,0,0,0,0,0,0,0,0]
# 16 → [0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0]
# (and output the result implicitly)
$endgroup$
05AB1E, 3 bytes
Ýo&
Port of @JonathanAllan's Jelly answer, so make sure to upvote him!
Contains zeros (including -loads of- trailing zeros).
Try it online or verify all test cases.
Explanation:
Ý # Create a list in the range [0, (implicit) input]
# i.e. 15 → [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
# i.e. 16 → [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
o # Take 2 to the power of each value
# → [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768]
# → [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536]
& # Bitwise-AND each value with the (implicit) input
# 15 → [1,2,4,8,0,0,0,0,0,0,0,0,0,0,0,0]
# 16 → [0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0]
# (and output the result implicitly)
edited Jan 29 at 12:27
answered Jan 29 at 7:31
Kevin CruijssenKevin Cruijssen
40k563206
40k563206
1
$begingroup$
... what?! Never honestly seenbitwise and
used in osabie. Nice one.
$endgroup$
– Magic Octopus Urn
Jan 29 at 15:40
$begingroup$
@MagicOctopusUrn I indeed also don't use it very often. Can't even find any other answer I've used&
in. xD I have used Bitwise-XOR a couple of times, like here or here and Bitwise-NOT once here (which I later removed again after golfing further..). I do use Bitwise-AND, XOR, OR, NOT, SHIFT, etc. pretty often in Java, but in 05AB1E not so much. :)
$endgroup$
– Kevin Cruijssen
Jan 29 at 15:55
add a comment |
1
$begingroup$
... what?! Never honestly seenbitwise and
used in osabie. Nice one.
$endgroup$
– Magic Octopus Urn
Jan 29 at 15:40
$begingroup$
@MagicOctopusUrn I indeed also don't use it very often. Can't even find any other answer I've used&
in. xD I have used Bitwise-XOR a couple of times, like here or here and Bitwise-NOT once here (which I later removed again after golfing further..). I do use Bitwise-AND, XOR, OR, NOT, SHIFT, etc. pretty often in Java, but in 05AB1E not so much. :)
$endgroup$
– Kevin Cruijssen
Jan 29 at 15:55
1
1
$begingroup$
... what?! Never honestly seen
bitwise and
used in osabie. Nice one.$endgroup$
– Magic Octopus Urn
Jan 29 at 15:40
$begingroup$
... what?! Never honestly seen
bitwise and
used in osabie. Nice one.$endgroup$
– Magic Octopus Urn
Jan 29 at 15:40
$begingroup$
@MagicOctopusUrn I indeed also don't use it very often. Can't even find any other answer I've used
&
in. xD I have used Bitwise-XOR a couple of times, like here or here and Bitwise-NOT once here (which I later removed again after golfing further..). I do use Bitwise-AND, XOR, OR, NOT, SHIFT, etc. pretty often in Java, but in 05AB1E not so much. :)$endgroup$
– Kevin Cruijssen
Jan 29 at 15:55
$begingroup$
@MagicOctopusUrn I indeed also don't use it very often. Can't even find any other answer I've used
&
in. xD I have used Bitwise-XOR a couple of times, like here or here and Bitwise-NOT once here (which I later removed again after golfing further..). I do use Bitwise-AND, XOR, OR, NOT, SHIFT, etc. pretty often in Java, but in 05AB1E not so much. :)$endgroup$
– Kevin Cruijssen
Jan 29 at 15:55
add a comment |
$begingroup$
Catholicon, 3 bytes
ṫĊŻ
Try it online!
Explanation:
ṫ Decompose into the largest values where:
Ċ the input
Ż the bit count is truthy (equal to one)
$endgroup$
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
Jan 28 at 22:25
$begingroup$
Works with 302231454903657293676544. Nice.
$endgroup$
– Michael Karas
Feb 3 at 13:54
add a comment |
$begingroup$
Catholicon, 3 bytes
ṫĊŻ
Try it online!
Explanation:
ṫ Decompose into the largest values where:
Ċ the input
Ż the bit count is truthy (equal to one)
$endgroup$
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
Jan 28 at 22:25
$begingroup$
Works with 302231454903657293676544. Nice.
$endgroup$
– Michael Karas
Feb 3 at 13:54
add a comment |
$begingroup$
Catholicon, 3 bytes
ṫĊŻ
Try it online!
Explanation:
ṫ Decompose into the largest values where:
Ċ the input
Ż the bit count is truthy (equal to one)
$endgroup$
Catholicon, 3 bytes
ṫĊŻ
Try it online!
Explanation:
ṫ Decompose into the largest values where:
Ċ the input
Ż the bit count is truthy (equal to one)
edited Jan 31 at 8:08
answered Jan 28 at 22:23
OkxOkx
12.9k128102
12.9k128102
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
Jan 28 at 22:25
$begingroup$
Works with 302231454903657293676544. Nice.
$endgroup$
– Michael Karas
Feb 3 at 13:54
add a comment |
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
Jan 28 at 22:25
$begingroup$
Works with 302231454903657293676544. Nice.
$endgroup$
– Michael Karas
Feb 3 at 13:54
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
Jan 28 at 22:25
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
Jan 28 at 22:25
$begingroup$
Works with 302231454903657293676544. Nice.
$endgroup$
– Michael Karas
Feb 3 at 13:54
$begingroup$
Works with 302231454903657293676544. Nice.
$endgroup$
– Michael Karas
Feb 3 at 13:54
add a comment |
$begingroup$
Wolfram Language (Mathematica), 17 bytes
#~NumberExpand~2&
Try it online!
Mathematica strikes again.
$endgroup$
$begingroup$
This also works with input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:56
add a comment |
$begingroup$
Wolfram Language (Mathematica), 17 bytes
#~NumberExpand~2&
Try it online!
Mathematica strikes again.
$endgroup$
$begingroup$
This also works with input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:56
add a comment |
$begingroup$
Wolfram Language (Mathematica), 17 bytes
#~NumberExpand~2&
Try it online!
Mathematica strikes again.
$endgroup$
Wolfram Language (Mathematica), 17 bytes
#~NumberExpand~2&
Try it online!
Mathematica strikes again.
answered Jan 28 at 22:57
lirtosiastlirtosiast
18.2k438109
18.2k438109
$begingroup$
This also works with input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:56
add a comment |
$begingroup$
This also works with input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:56
$begingroup$
This also works with input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:56
$begingroup$
This also works with input of 302231454903657293676544.
$endgroup$
– Michael Karas
Feb 3 at 13:56
add a comment |
$begingroup$
R, 27 23 bytes
bitwAnd(scan(),2^(7:0))
Try it online!
Unrolled code and explanation :
A = scan() # get input number A from stdin
# e.g. A = 65
bitwAnd( A , 2^(7:0)) # bitwise AND between all powers of 2 : 2^7 ... 2^0 and A
# and implicitly print the result
# e.g. B = bitwAnd(65, c(128,64,32,16,8,4,2,1)) = c(0,64,0,0,0,0,0,1)
- 4 bytes thanks to @Kirill L.
$endgroup$
1
$begingroup$
23 bytes with bitwise and.
$endgroup$
– Kirill L.
Jan 29 at 9:53
$begingroup$
@KirillL.: brilliant !
$endgroup$
– digEmAll
Jan 29 at 13:16
add a comment |
$begingroup$
R, 27 23 bytes
bitwAnd(scan(),2^(7:0))
Try it online!
Unrolled code and explanation :
A = scan() # get input number A from stdin
# e.g. A = 65
bitwAnd( A , 2^(7:0)) # bitwise AND between all powers of 2 : 2^7 ... 2^0 and A
# and implicitly print the result
# e.g. B = bitwAnd(65, c(128,64,32,16,8,4,2,1)) = c(0,64,0,0,0,0,0,1)
- 4 bytes thanks to @Kirill L.
$endgroup$
1
$begingroup$
23 bytes with bitwise and.
$endgroup$
– Kirill L.
Jan 29 at 9:53
$begingroup$
@KirillL.: brilliant !
$endgroup$
– digEmAll
Jan 29 at 13:16
add a comment |
$begingroup$
R, 27 23 bytes
bitwAnd(scan(),2^(7:0))
Try it online!
Unrolled code and explanation :
A = scan() # get input number A from stdin
# e.g. A = 65
bitwAnd( A , 2^(7:0)) # bitwise AND between all powers of 2 : 2^7 ... 2^0 and A
# and implicitly print the result
# e.g. B = bitwAnd(65, c(128,64,32,16,8,4,2,1)) = c(0,64,0,0,0,0,0,1)
- 4 bytes thanks to @Kirill L.
$endgroup$
R, 27 23 bytes
bitwAnd(scan(),2^(7:0))
Try it online!
Unrolled code and explanation :
A = scan() # get input number A from stdin
# e.g. A = 65
bitwAnd( A , 2^(7:0)) # bitwise AND between all powers of 2 : 2^7 ... 2^0 and A
# and implicitly print the result
# e.g. B = bitwAnd(65, c(128,64,32,16,8,4,2,1)) = c(0,64,0,0,0,0,0,1)
- 4 bytes thanks to @Kirill L.
edited Jan 29 at 14:35
answered Jan 29 at 7:44
digEmAlldigEmAll
3,444415
3,444415
1
$begingroup$
23 bytes with bitwise and.
$endgroup$
– Kirill L.
Jan 29 at 9:53
$begingroup$
@KirillL.: brilliant !
$endgroup$
– digEmAll
Jan 29 at 13:16
add a comment |
1
$begingroup$
23 bytes with bitwise and.
$endgroup$
– Kirill L.
Jan 29 at 9:53
$begingroup$
@KirillL.: brilliant !
$endgroup$
– digEmAll
Jan 29 at 13:16
1
1
$begingroup$
23 bytes with bitwise and.
$endgroup$
– Kirill L.
Jan 29 at 9:53
$begingroup$
23 bytes with bitwise and.
$endgroup$
– Kirill L.
Jan 29 at 9:53
$begingroup$
@KirillL.: brilliant !
$endgroup$
– digEmAll
Jan 29 at 13:16
$begingroup$
@KirillL.: brilliant !
$endgroup$
– digEmAll
Jan 29 at 13:16
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 29 bytes
Contains 5 unprintable characters.
n=>"@ ".Select(a=>a&n)
Explanation
//Lambda taking one parameter 'n'
n=>
//String with ASCII characters 128, 64, 32, 16, 8, 4, 2, and 1
"@ "
//Iterate through all the chars of the above string and transform them to
.Select(a=>
//A bitwise AND operation between the integer value of the current char and the input value
a&n)
Try it online!
$endgroup$
$begingroup$
But we need to get rid of zeros, liken=>new int[8].Select((j,i)=>1<<i&n).Where(i=>i!=0)
The part beforeWhere
is five bytes shorter btw
$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 8:49
$begingroup$
@polfosolThe output may contain zeros
$endgroup$
– Jo King
Jan 29 at 9:06
2
$begingroup$
@JoKing Still,n=>new int[8].Select((j,i)=>1<<i&n)
is 35 bytes long and we won't need additional flags and text encodings.
$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 13:02
1
$begingroup$
Using ascii characters 0-7 should be shorter, egn=>"INSERT ASCII HERE".Select(a=>1<<a&n)
But I'm on a mobile device that can't display or type unprintables, so I'll have to wait till I get home to update the answer
$endgroup$
– Embodiment of Ignorance
Jan 29 at 16:34
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 29 bytes
Contains 5 unprintable characters.
n=>"@ ".Select(a=>a&n)
Explanation
//Lambda taking one parameter 'n'
n=>
//String with ASCII characters 128, 64, 32, 16, 8, 4, 2, and 1
"@ "
//Iterate through all the chars of the above string and transform them to
.Select(a=>
//A bitwise AND operation between the integer value of the current char and the input value
a&n)
Try it online!
$endgroup$
$begingroup$
But we need to get rid of zeros, liken=>new int[8].Select((j,i)=>1<<i&n).Where(i=>i!=0)
The part beforeWhere
is five bytes shorter btw
$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 8:49
$begingroup$
@polfosolThe output may contain zeros
$endgroup$
– Jo King
Jan 29 at 9:06
2
$begingroup$
@JoKing Still,n=>new int[8].Select((j,i)=>1<<i&n)
is 35 bytes long and we won't need additional flags and text encodings.
$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 13:02
1
$begingroup$
Using ascii characters 0-7 should be shorter, egn=>"INSERT ASCII HERE".Select(a=>1<<a&n)
But I'm on a mobile device that can't display or type unprintables, so I'll have to wait till I get home to update the answer
$endgroup$
– Embodiment of Ignorance
Jan 29 at 16:34
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 29 bytes
Contains 5 unprintable characters.
n=>"@ ".Select(a=>a&n)
Explanation
//Lambda taking one parameter 'n'
n=>
//String with ASCII characters 128, 64, 32, 16, 8, 4, 2, and 1
"@ "
//Iterate through all the chars of the above string and transform them to
.Select(a=>
//A bitwise AND operation between the integer value of the current char and the input value
a&n)
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 29 bytes
Contains 5 unprintable characters.
n=>"@ ".Select(a=>a&n)
Explanation
//Lambda taking one parameter 'n'
n=>
//String with ASCII characters 128, 64, 32, 16, 8, 4, 2, and 1
"@ "
//Iterate through all the chars of the above string and transform them to
.Select(a=>
//A bitwise AND operation between the integer value of the current char and the input value
a&n)
Try it online!
edited Jan 30 at 4:05
answered Jan 29 at 4:26
Embodiment of IgnoranceEmbodiment of Ignorance
1,536124
1,536124
$begingroup$
But we need to get rid of zeros, liken=>new int[8].Select((j,i)=>1<<i&n).Where(i=>i!=0)
The part beforeWhere
is five bytes shorter btw
$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 8:49
$begingroup$
@polfosolThe output may contain zeros
$endgroup$
– Jo King
Jan 29 at 9:06
2
$begingroup$
@JoKing Still,n=>new int[8].Select((j,i)=>1<<i&n)
is 35 bytes long and we won't need additional flags and text encodings.
$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 13:02
1
$begingroup$
Using ascii characters 0-7 should be shorter, egn=>"INSERT ASCII HERE".Select(a=>1<<a&n)
But I'm on a mobile device that can't display or type unprintables, so I'll have to wait till I get home to update the answer
$endgroup$
– Embodiment of Ignorance
Jan 29 at 16:34
add a comment |
$begingroup$
But we need to get rid of zeros, liken=>new int[8].Select((j,i)=>1<<i&n).Where(i=>i!=0)
The part beforeWhere
is five bytes shorter btw
$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 8:49
$begingroup$
@polfosolThe output may contain zeros
$endgroup$
– Jo King
Jan 29 at 9:06
2
$begingroup$
@JoKing Still,n=>new int[8].Select((j,i)=>1<<i&n)
is 35 bytes long and we won't need additional flags and text encodings.
$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 13:02
1
$begingroup$
Using ascii characters 0-7 should be shorter, egn=>"INSERT ASCII HERE".Select(a=>1<<a&n)
But I'm on a mobile device that can't display or type unprintables, so I'll have to wait till I get home to update the answer
$endgroup$
– Embodiment of Ignorance
Jan 29 at 16:34
$begingroup$
But we need to get rid of zeros, like
n=>new int[8].Select((j,i)=>1<<i&n).Where(i=>i!=0)
The part before Where
is five bytes shorter btw$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 8:49
$begingroup$
But we need to get rid of zeros, like
n=>new int[8].Select((j,i)=>1<<i&n).Where(i=>i!=0)
The part before Where
is five bytes shorter btw$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 8:49
$begingroup$
@polfosol
The output may contain zeros
$endgroup$
– Jo King
Jan 29 at 9:06
$begingroup$
@polfosol
The output may contain zeros
$endgroup$
– Jo King
Jan 29 at 9:06
2
2
$begingroup$
@JoKing Still,
n=>new int[8].Select((j,i)=>1<<i&n)
is 35 bytes long and we won't need additional flags and text encodings.$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 13:02
$begingroup$
@JoKing Still,
n=>new int[8].Select((j,i)=>1<<i&n)
is 35 bytes long and we won't need additional flags and text encodings.$endgroup$
– polfosol ఠ_ఠ
Jan 29 at 13:02
1
1
$begingroup$
Using ascii characters 0-7 should be shorter, eg
n=>"INSERT ASCII HERE".Select(a=>1<<a&n)
But I'm on a mobile device that can't display or type unprintables, so I'll have to wait till I get home to update the answer$endgroup$
– Embodiment of Ignorance
Jan 29 at 16:34
$begingroup$
Using ascii characters 0-7 should be shorter, eg
n=>"INSERT ASCII HERE".Select(a=>1<<a&n)
But I'm on a mobile device that can't display or type unprintables, so I'll have to wait till I get home to update the answer$endgroup$
– Embodiment of Ignorance
Jan 29 at 16:34
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 38 bytes
x=>{for(int y=8;y-->0;Print(x&1<<y));}
Try it online!
$endgroup$
$begingroup$
aw, close :P
$endgroup$
– ASCII-only
Jan 29 at 7:45
1
$begingroup$
Fails for inputs1
,2
,4
,8
,16
, etc. (thex>y
should bex>=y
instead).
$endgroup$
– Kevin Cruijssen
Jan 29 at 9:37
1
$begingroup$
@ASCIIOnly - I'm telling you, the range operator is going to be sweet :)
$endgroup$
– dana
Jan 29 at 13:43
$begingroup$
@ASCII-only Mean while, you can use the flag/u:System.Linq.Enumerable
and try this for 31 bytes
$endgroup$
– Embodiment of Ignorance
Jan 29 at 23:18
$begingroup$
@EmbodimentofIgnorance sure. but i'd prefer not to list language as "C#/u:System.Linq.Enumerable
" :P
$endgroup$
– ASCII-only
Jan 30 at 4:54
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 38 bytes
x=>{for(int y=8;y-->0;Print(x&1<<y));}
Try it online!
$endgroup$
$begingroup$
aw, close :P
$endgroup$
– ASCII-only
Jan 29 at 7:45
1
$begingroup$
Fails for inputs1
,2
,4
,8
,16
, etc. (thex>y
should bex>=y
instead).
$endgroup$
– Kevin Cruijssen
Jan 29 at 9:37
1
$begingroup$
@ASCIIOnly - I'm telling you, the range operator is going to be sweet :)
$endgroup$
– dana
Jan 29 at 13:43
$begingroup$
@ASCII-only Mean while, you can use the flag/u:System.Linq.Enumerable
and try this for 31 bytes
$endgroup$
– Embodiment of Ignorance
Jan 29 at 23:18
$begingroup$
@EmbodimentofIgnorance sure. but i'd prefer not to list language as "C#/u:System.Linq.Enumerable
" :P
$endgroup$
– ASCII-only
Jan 30 at 4:54
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 38 bytes
x=>{for(int y=8;y-->0;Print(x&1<<y));}
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 38 bytes
x=>{for(int y=8;y-->0;Print(x&1<<y));}
Try it online!
edited Jan 29 at 13:55
answered Jan 29 at 6:16
danadana
1,471167
1,471167
$begingroup$
aw, close :P
$endgroup$
– ASCII-only
Jan 29 at 7:45
1
$begingroup$
Fails for inputs1
,2
,4
,8
,16
, etc. (thex>y
should bex>=y
instead).
$endgroup$
– Kevin Cruijssen
Jan 29 at 9:37
1
$begingroup$
@ASCIIOnly - I'm telling you, the range operator is going to be sweet :)
$endgroup$
– dana
Jan 29 at 13:43
$begingroup$
@ASCII-only Mean while, you can use the flag/u:System.Linq.Enumerable
and try this for 31 bytes
$endgroup$
– Embodiment of Ignorance
Jan 29 at 23:18
$begingroup$
@EmbodimentofIgnorance sure. but i'd prefer not to list language as "C#/u:System.Linq.Enumerable
" :P
$endgroup$
– ASCII-only
Jan 30 at 4:54
add a comment |
$begingroup$
aw, close :P
$endgroup$
– ASCII-only
Jan 29 at 7:45
1
$begingroup$
Fails for inputs1
,2
,4
,8
,16
, etc. (thex>y
should bex>=y
instead).
$endgroup$
– Kevin Cruijssen
Jan 29 at 9:37
1
$begingroup$
@ASCIIOnly - I'm telling you, the range operator is going to be sweet :)
$endgroup$
– dana
Jan 29 at 13:43
$begingroup$
@ASCII-only Mean while, you can use the flag/u:System.Linq.Enumerable
and try this for 31 bytes
$endgroup$
– Embodiment of Ignorance
Jan 29 at 23:18
$begingroup$
@EmbodimentofIgnorance sure. but i'd prefer not to list language as "C#/u:System.Linq.Enumerable
" :P
$endgroup$
– ASCII-only
Jan 30 at 4:54
$begingroup$
aw, close :P
$endgroup$
– ASCII-only
Jan 29 at 7:45
$begingroup$
aw, close :P
$endgroup$
– ASCII-only
Jan 29 at 7:45
1
1
$begingroup$
Fails for inputs
1
, 2
, 4
, 8
, 16
, etc. (the x>y
should be x>=y
instead).$endgroup$
– Kevin Cruijssen
Jan 29 at 9:37
$begingroup$
Fails for inputs
1
, 2
, 4
, 8
, 16
, etc. (the x>y
should be x>=y
instead).$endgroup$
– Kevin Cruijssen
Jan 29 at 9:37
1
1
$begingroup$
@ASCIIOnly - I'm telling you, the range operator is going to be sweet :)
$endgroup$
– dana
Jan 29 at 13:43
$begingroup$
@ASCIIOnly - I'm telling you, the range operator is going to be sweet :)
$endgroup$
– dana
Jan 29 at 13:43
$begingroup$
@ASCII-only Mean while, you can use the flag
/u:System.Linq.Enumerable
and try this for 31 bytes$endgroup$
– Embodiment of Ignorance
Jan 29 at 23:18
$begingroup$
@ASCII-only Mean while, you can use the flag
/u:System.Linq.Enumerable
and try this for 31 bytes$endgroup$
– Embodiment of Ignorance
Jan 29 at 23:18
$begingroup$
@EmbodimentofIgnorance sure. but i'd prefer not to list language as "C#
/u:System.Linq.Enumerable
" :P$endgroup$
– ASCII-only
Jan 30 at 4:54
$begingroup$
@EmbodimentofIgnorance sure. but i'd prefer not to list language as "C#
/u:System.Linq.Enumerable
" :P$endgroup$
– ASCII-only
Jan 30 at 4:54
add a comment |
$begingroup$
C (gcc), 39 bytes
f(n){for(;n;n&=n-1)printf("%d ",n&-n);}
Try it online!
$endgroup$
add a comment |
$begingroup$
C (gcc), 39 bytes
f(n){for(;n;n&=n-1)printf("%d ",n&-n);}
Try it online!
$endgroup$
add a comment |
$begingroup$
C (gcc), 39 bytes
f(n){for(;n;n&=n-1)printf("%d ",n&-n);}
Try it online!
$endgroup$
C (gcc), 39 bytes
f(n){for(;n;n&=n-1)printf("%d ",n&-n);}
Try it online!
answered Jan 29 at 10:43
nwellnhofnwellnhof
7,27511128
7,27511128
add a comment |
add a comment |
$begingroup$
05AB1E, 7 bytes
2вRƶ<oò
explanation:
2в convert input to binary array
R reverse array
ƶ< multiply each item by it's index and subtract 1
oò 2^item then round down
Try it online!
$endgroup$
$begingroup$
Also works with input of 302231454903657293676544
$endgroup$
– Michael Karas
Feb 3 at 14:00
add a comment |
$begingroup$
05AB1E, 7 bytes
2вRƶ<oò
explanation:
2в convert input to binary array
R reverse array
ƶ< multiply each item by it's index and subtract 1
oò 2^item then round down
Try it online!
$endgroup$
$begingroup$
Also works with input of 302231454903657293676544
$endgroup$
– Michael Karas
Feb 3 at 14:00
add a comment |
$begingroup$
05AB1E, 7 bytes
2вRƶ<oò
explanation:
2в convert input to binary array
R reverse array
ƶ< multiply each item by it's index and subtract 1
oò 2^item then round down
Try it online!
$endgroup$
05AB1E, 7 bytes
2вRƶ<oò
explanation:
2в convert input to binary array
R reverse array
ƶ< multiply each item by it's index and subtract 1
oò 2^item then round down
Try it online!
edited Jan 29 at 15:50
Shaggy
19.4k21667
19.4k21667
answered Jan 29 at 6:05
JacksonJackson
811
811
$begingroup$
Also works with input of 302231454903657293676544
$endgroup$
– Michael Karas
Feb 3 at 14:00
add a comment |
$begingroup$
Also works with input of 302231454903657293676544
$endgroup$
– Michael Karas
Feb 3 at 14:00
$begingroup$
Also works with input of 302231454903657293676544
$endgroup$
– Michael Karas
Feb 3 at 14:00
$begingroup$
Also works with input of 302231454903657293676544
$endgroup$
– Michael Karas
Feb 3 at 14:00
add a comment |
$begingroup$
Haskell, 29 bytes
(mapM(n->[0,2^n])[7,6..0]!!)
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 29 bytes
(mapM(n->[0,2^n])[7,6..0]!!)
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 29 bytes
(mapM(n->[0,2^n])[7,6..0]!!)
Try it online!
$endgroup$
Haskell, 29 bytes
(mapM(n->[0,2^n])[7,6..0]!!)
Try it online!
answered Jan 29 at 23:45
xnorxnor
91.8k18187444
91.8k18187444
add a comment |
add a comment |
$begingroup$
C (clang), 133 110 63 58 bytes
58-byte solution thanks to @ceilingcat.
x=256;main(y){for(scanf("%d",&y);x/=2;)printf("%d ",y&x);}
Try it online!
$endgroup$
$begingroup$
In C89, you can declare likemain(){}
and the return type defaults to int. Same for variables at global scope. Also, at least on normal implementations like clang, printf and scanf work without prototypes. You get warnings of course, but it's still valid C89 (maybe) or at least K&R C for them to be implicitly declared. The types of the C objects you pass as args defines how they're passed, so achar*
andint*
will Just Work without truncating pointers to 32-bit on x86-64 or anything. (Default argument promotions happen, same as for variadic functions which they are anyway.)
$endgroup$
– Peter Cordes
Jan 30 at 3:46
$begingroup$
Or is this aiming to be valid C11 with no undefined behaviour? If so, proudly proclaim it. :) And BTW, writing a function that takes an output array as an arg would probably be smaller. Anyway, see Tips for golfing in C
$endgroup$
– Peter Cordes
Jan 30 at 3:48
$begingroup$
You can use bitwise&
to check if a bit is set. Likey&(1<<x)&&printf("%d ",1<<x);
. Or to not skip zeros, justprintf("%d ", y&(1<<x))
. Or instead of counting bit positions, usex=256
andx>>=1
to shift the mask.main(y){int x=256;for(scanf("%d",&y);x>>=1;)printf("%d ",y&x);}
63 bytes Try it online! clang will even compile that with-std=c11
$endgroup$
– Peter Cordes
Jan 30 at 4:11
$begingroup$
44 bytes
$endgroup$
– ceilingcat
Feb 9 at 1:25
add a comment |
$begingroup$
C (clang), 133 110 63 58 bytes
58-byte solution thanks to @ceilingcat.
x=256;main(y){for(scanf("%d",&y);x/=2;)printf("%d ",y&x);}
Try it online!
$endgroup$
$begingroup$
In C89, you can declare likemain(){}
and the return type defaults to int. Same for variables at global scope. Also, at least on normal implementations like clang, printf and scanf work without prototypes. You get warnings of course, but it's still valid C89 (maybe) or at least K&R C for them to be implicitly declared. The types of the C objects you pass as args defines how they're passed, so achar*
andint*
will Just Work without truncating pointers to 32-bit on x86-64 or anything. (Default argument promotions happen, same as for variadic functions which they are anyway.)
$endgroup$
– Peter Cordes
Jan 30 at 3:46
$begingroup$
Or is this aiming to be valid C11 with no undefined behaviour? If so, proudly proclaim it. :) And BTW, writing a function that takes an output array as an arg would probably be smaller. Anyway, see Tips for golfing in C
$endgroup$
– Peter Cordes
Jan 30 at 3:48
$begingroup$
You can use bitwise&
to check if a bit is set. Likey&(1<<x)&&printf("%d ",1<<x);
. Or to not skip zeros, justprintf("%d ", y&(1<<x))
. Or instead of counting bit positions, usex=256
andx>>=1
to shift the mask.main(y){int x=256;for(scanf("%d",&y);x>>=1;)printf("%d ",y&x);}
63 bytes Try it online! clang will even compile that with-std=c11
$endgroup$
– Peter Cordes
Jan 30 at 4:11
$begingroup$
44 bytes
$endgroup$
– ceilingcat
Feb 9 at 1:25
add a comment |
$begingroup$
C (clang), 133 110 63 58 bytes
58-byte solution thanks to @ceilingcat.
x=256;main(y){for(scanf("%d",&y);x/=2;)printf("%d ",y&x);}
Try it online!
$endgroup$
C (clang), 133 110 63 58 bytes
58-byte solution thanks to @ceilingcat.
x=256;main(y){for(scanf("%d",&y);x/=2;)printf("%d ",y&x);}
Try it online!
edited Feb 9 at 1:20
answered Jan 29 at 3:57
a stone arachnida stone arachnid
1836
1836
$begingroup$
In C89, you can declare likemain(){}
and the return type defaults to int. Same for variables at global scope. Also, at least on normal implementations like clang, printf and scanf work without prototypes. You get warnings of course, but it's still valid C89 (maybe) or at least K&R C for them to be implicitly declared. The types of the C objects you pass as args defines how they're passed, so achar*
andint*
will Just Work without truncating pointers to 32-bit on x86-64 or anything. (Default argument promotions happen, same as for variadic functions which they are anyway.)
$endgroup$
– Peter Cordes
Jan 30 at 3:46
$begingroup$
Or is this aiming to be valid C11 with no undefined behaviour? If so, proudly proclaim it. :) And BTW, writing a function that takes an output array as an arg would probably be smaller. Anyway, see Tips for golfing in C
$endgroup$
– Peter Cordes
Jan 30 at 3:48
$begingroup$
You can use bitwise&
to check if a bit is set. Likey&(1<<x)&&printf("%d ",1<<x);
. Or to not skip zeros, justprintf("%d ", y&(1<<x))
. Or instead of counting bit positions, usex=256
andx>>=1
to shift the mask.main(y){int x=256;for(scanf("%d",&y);x>>=1;)printf("%d ",y&x);}
63 bytes Try it online! clang will even compile that with-std=c11
$endgroup$
– Peter Cordes
Jan 30 at 4:11
$begingroup$
44 bytes
$endgroup$
– ceilingcat
Feb 9 at 1:25
add a comment |
$begingroup$
In C89, you can declare likemain(){}
and the return type defaults to int. Same for variables at global scope. Also, at least on normal implementations like clang, printf and scanf work without prototypes. You get warnings of course, but it's still valid C89 (maybe) or at least K&R C for them to be implicitly declared. The types of the C objects you pass as args defines how they're passed, so achar*
andint*
will Just Work without truncating pointers to 32-bit on x86-64 or anything. (Default argument promotions happen, same as for variadic functions which they are anyway.)
$endgroup$
– Peter Cordes
Jan 30 at 3:46
$begingroup$
Or is this aiming to be valid C11 with no undefined behaviour? If so, proudly proclaim it. :) And BTW, writing a function that takes an output array as an arg would probably be smaller. Anyway, see Tips for golfing in C
$endgroup$
– Peter Cordes
Jan 30 at 3:48
$begingroup$
You can use bitwise&
to check if a bit is set. Likey&(1<<x)&&printf("%d ",1<<x);
. Or to not skip zeros, justprintf("%d ", y&(1<<x))
. Or instead of counting bit positions, usex=256
andx>>=1
to shift the mask.main(y){int x=256;for(scanf("%d",&y);x>>=1;)printf("%d ",y&x);}
63 bytes Try it online! clang will even compile that with-std=c11
$endgroup$
– Peter Cordes
Jan 30 at 4:11
$begingroup$
44 bytes
$endgroup$
– ceilingcat
Feb 9 at 1:25
$begingroup$
In C89, you can declare like
main(){}
and the return type defaults to int. Same for variables at global scope. Also, at least on normal implementations like clang, printf and scanf work without prototypes. You get warnings of course, but it's still valid C89 (maybe) or at least K&R C for them to be implicitly declared. The types of the C objects you pass as args defines how they're passed, so a char*
and int*
will Just Work without truncating pointers to 32-bit on x86-64 or anything. (Default argument promotions happen, same as for variadic functions which they are anyway.)$endgroup$
– Peter Cordes
Jan 30 at 3:46
$begingroup$
In C89, you can declare like
main(){}
and the return type defaults to int. Same for variables at global scope. Also, at least on normal implementations like clang, printf and scanf work without prototypes. You get warnings of course, but it's still valid C89 (maybe) or at least K&R C for them to be implicitly declared. The types of the C objects you pass as args defines how they're passed, so a char*
and int*
will Just Work without truncating pointers to 32-bit on x86-64 or anything. (Default argument promotions happen, same as for variadic functions which they are anyway.)$endgroup$
– Peter Cordes
Jan 30 at 3:46
$begingroup$
Or is this aiming to be valid C11 with no undefined behaviour? If so, proudly proclaim it. :) And BTW, writing a function that takes an output array as an arg would probably be smaller. Anyway, see Tips for golfing in C
$endgroup$
– Peter Cordes
Jan 30 at 3:48
$begingroup$
Or is this aiming to be valid C11 with no undefined behaviour? If so, proudly proclaim it. :) And BTW, writing a function that takes an output array as an arg would probably be smaller. Anyway, see Tips for golfing in C
$endgroup$
– Peter Cordes
Jan 30 at 3:48
$begingroup$
You can use bitwise
&
to check if a bit is set. Like y&(1<<x)&&printf("%d ",1<<x);
. Or to not skip zeros, just printf("%d ", y&(1<<x))
. Or instead of counting bit positions, use x=256
and x>>=1
to shift the mask. main(y){int x=256;for(scanf("%d",&y);x>>=1;)printf("%d ",y&x);}
63 bytes Try it online! clang will even compile that with -std=c11
$endgroup$
– Peter Cordes
Jan 30 at 4:11
$begingroup$
You can use bitwise
&
to check if a bit is set. Like y&(1<<x)&&printf("%d ",1<<x);
. Or to not skip zeros, just printf("%d ", y&(1<<x))
. Or instead of counting bit positions, use x=256
and x>>=1
to shift the mask. main(y){int x=256;for(scanf("%d",&y);x>>=1;)printf("%d ",y&x);}
63 bytes Try it online! clang will even compile that with -std=c11
$endgroup$
– Peter Cordes
Jan 30 at 4:11
$begingroup$
44 bytes
$endgroup$
– ceilingcat
Feb 9 at 1:25
$begingroup$
44 bytes
$endgroup$
– ceilingcat
Feb 9 at 1:25
add a comment |
$begingroup$
MATL, 5 bytes
BPfqW
Try it online!
Explanation
Consider input 86
as an example.
B % Implicit input. Convert to binary (highest to lowest digits)
% STACK: [1 0 1 0 1 1 0]
P % Flip
% STACK: [0 1 1 0 1 0 1]
f % Find: indices of nonzeros (1-based)
% STACK: [2 3 5 7]
q % Subtract 1, element-wise
% STACK: [1 2 4 6]
W % Exponential with base 2, element-wise. Implicit display
% STACK: [2 4 16 64]
$endgroup$
add a comment |
$begingroup$
MATL, 5 bytes
BPfqW
Try it online!
Explanation
Consider input 86
as an example.
B % Implicit input. Convert to binary (highest to lowest digits)
% STACK: [1 0 1 0 1 1 0]
P % Flip
% STACK: [0 1 1 0 1 0 1]
f % Find: indices of nonzeros (1-based)
% STACK: [2 3 5 7]
q % Subtract 1, element-wise
% STACK: [1 2 4 6]
W % Exponential with base 2, element-wise. Implicit display
% STACK: [2 4 16 64]
$endgroup$
add a comment |
$begingroup$
MATL, 5 bytes
BPfqW
Try it online!
Explanation
Consider input 86
as an example.
B % Implicit input. Convert to binary (highest to lowest digits)
% STACK: [1 0 1 0 1 1 0]
P % Flip
% STACK: [0 1 1 0 1 0 1]
f % Find: indices of nonzeros (1-based)
% STACK: [2 3 5 7]
q % Subtract 1, element-wise
% STACK: [1 2 4 6]
W % Exponential with base 2, element-wise. Implicit display
% STACK: [2 4 16 64]
$endgroup$
MATL, 5 bytes
BPfqW
Try it online!
Explanation
Consider input 86
as an example.
B % Implicit input. Convert to binary (highest to lowest digits)
% STACK: [1 0 1 0 1 1 0]
P % Flip
% STACK: [0 1 1 0 1 0 1]
f % Find: indices of nonzeros (1-based)
% STACK: [2 3 5 7]
q % Subtract 1, element-wise
% STACK: [1 2 4 6]
W % Exponential with base 2, element-wise. Implicit display
% STACK: [2 4 16 64]
answered Jan 28 at 22:38
Luis MendoLuis Mendo
74.7k888291
74.7k888291
add a comment |
add a comment |
$begingroup$
Perl 6, 16 12 bytes
-4 bytes thanks to Jonathan Allan
*+&2**all ^8
Try it online!
Returns an All Junction with 8 elements. This is a rather non-standard way of returning, but generally, Junctions can act as ordered (at least until autothreading is implemented) lists and it is possible to extract the values from one.
Explanation:
*+& # Bitwise AND the input with
2** # 2 raised to the power of
all ^8 # All of the range 0 to 7
$endgroup$
add a comment |
$begingroup$
Perl 6, 16 12 bytes
-4 bytes thanks to Jonathan Allan
*+&2**all ^8
Try it online!
Returns an All Junction with 8 elements. This is a rather non-standard way of returning, but generally, Junctions can act as ordered (at least until autothreading is implemented) lists and it is possible to extract the values from one.
Explanation:
*+& # Bitwise AND the input with
2** # 2 raised to the power of
all ^8 # All of the range 0 to 7
$endgroup$
add a comment |
$begingroup$
Perl 6, 16 12 bytes
-4 bytes thanks to Jonathan Allan
*+&2**all ^8
Try it online!
Returns an All Junction with 8 elements. This is a rather non-standard way of returning, but generally, Junctions can act as ordered (at least until autothreading is implemented) lists and it is possible to extract the values from one.
Explanation:
*+& # Bitwise AND the input with
2** # 2 raised to the power of
all ^8 # All of the range 0 to 7
$endgroup$
Perl 6, 16 12 bytes
-4 bytes thanks to Jonathan Allan
*+&2**all ^8
Try it online!
Returns an All Junction with 8 elements. This is a rather non-standard way of returning, but generally, Junctions can act as ordered (at least until autothreading is implemented) lists and it is possible to extract the values from one.
Explanation:
*+& # Bitwise AND the input with
2** # 2 raised to the power of
all ^8 # All of the range 0 to 7
edited Jan 29 at 9:16
answered Jan 28 at 22:35
Jo KingJo King
24.4k357126
24.4k357126
add a comment |
add a comment |
$begingroup$
Japt, 8 5 bytes
Æ&2pX
Try it
Æ&2pX :Implicit input of integer U
Æ :Map each X in the range [0,U)
& : Bitwise AND of U with
2pX : 2 to the power of X
Alternative
Suggested by Oliver to avoid the 0
s in the output using the -mf
flag.
N&2pU
Try it
N&2pU :Implicitly map each U in the range [0,input)
N :The (singleton) array of inputs
& :Bitwise AND with
2pX :2 to the power of U
:Implicitly filter and output
$endgroup$
1
$begingroup$
Nice one. You can doN&2pU
with-mf
to avoid the0
s
$endgroup$
– Oliver
Jan 29 at 2:54
add a comment |
$begingroup$
Japt, 8 5 bytes
Æ&2pX
Try it
Æ&2pX :Implicit input of integer U
Æ :Map each X in the range [0,U)
& : Bitwise AND of U with
2pX : 2 to the power of X
Alternative
Suggested by Oliver to avoid the 0
s in the output using the -mf
flag.
N&2pU
Try it
N&2pU :Implicitly map each U in the range [0,input)
N :The (singleton) array of inputs
& :Bitwise AND with
2pX :2 to the power of U
:Implicitly filter and output
$endgroup$
1
$begingroup$
Nice one. You can doN&2pU
with-mf
to avoid the0
s
$endgroup$
– Oliver
Jan 29 at 2:54
add a comment |
$begingroup$
Japt, 8 5 bytes
Æ&2pX
Try it
Æ&2pX :Implicit input of integer U
Æ :Map each X in the range [0,U)
& : Bitwise AND of U with
2pX : 2 to the power of X
Alternative
Suggested by Oliver to avoid the 0
s in the output using the -mf
flag.
N&2pU
Try it
N&2pU :Implicitly map each U in the range [0,input)
N :The (singleton) array of inputs
& :Bitwise AND with
2pX :2 to the power of U
:Implicitly filter and output
$endgroup$
Japt, 8 5 bytes
Æ&2pX
Try it
Æ&2pX :Implicit input of integer U
Æ :Map each X in the range [0,U)
& : Bitwise AND of U with
2pX : 2 to the power of X
Alternative
Suggested by Oliver to avoid the 0
s in the output using the -mf
flag.
N&2pU
Try it
N&2pU :Implicitly map each U in the range [0,input)
N :The (singleton) array of inputs
& :Bitwise AND with
2pX :2 to the power of U
:Implicitly filter and output
edited Jan 29 at 11:42
answered Jan 28 at 22:12
ShaggyShaggy
19.4k21667
19.4k21667
1
$begingroup$
Nice one. You can doN&2pU
with-mf
to avoid the0
s
$endgroup$
– Oliver
Jan 29 at 2:54
add a comment |
1
$begingroup$
Nice one. You can doN&2pU
with-mf
to avoid the0
s
$endgroup$
– Oliver
Jan 29 at 2:54
1
1
$begingroup$
Nice one. You can do
N&2pU
with -mf
to avoid the 0
s$endgroup$
– Oliver
Jan 29 at 2:54
$begingroup$
Nice one. You can do
N&2pU
with -mf
to avoid the 0
s$endgroup$
– Oliver
Jan 29 at 2:54
add a comment |
$begingroup$
05AB1E, 9 bytes
Ýoʒ›}æʒOQ
Try it online!
This is also correct for 6-bytes, but it doesn't complete in time on TIO for 86:
05AB1E, 6 bytes
ÝoæʒOQ
Try it online!
$endgroup$
1
$begingroup$
Both your answers output an empty set for15
, instead of[1,2,4,8]
$endgroup$
– Kevin Cruijssen
Jan 29 at 7:14
1
$begingroup$
@KevinCruijssen needed2**0
, nice catch.Ý
overL
.
$endgroup$
– Magic Octopus Urn
Jan 29 at 15:29
1
$begingroup$
Ah, I know the feeling. Also hadL
instead ofÝ
at first in my answer.
$endgroup$
– Kevin Cruijssen
Jan 29 at 15:31
add a comment |
$begingroup$
05AB1E, 9 bytes
Ýoʒ›}æʒOQ
Try it online!
This is also correct for 6-bytes, but it doesn't complete in time on TIO for 86:
05AB1E, 6 bytes
ÝoæʒOQ
Try it online!
$endgroup$
1
$begingroup$
Both your answers output an empty set for15
, instead of[1,2,4,8]
$endgroup$
– Kevin Cruijssen
Jan 29 at 7:14
1
$begingroup$
@KevinCruijssen needed2**0
, nice catch.Ý
overL
.
$endgroup$
– Magic Octopus Urn
Jan 29 at 15:29
1
$begingroup$
Ah, I know the feeling. Also hadL
instead ofÝ
at first in my answer.
$endgroup$
– Kevin Cruijssen
Jan 29 at 15:31
add a comment |
$begingroup$
05AB1E, 9 bytes
Ýoʒ›}æʒOQ
Try it online!
This is also correct for 6-bytes, but it doesn't complete in time on TIO for 86:
05AB1E, 6 bytes
ÝoæʒOQ
Try it online!
$endgroup$
05AB1E, 9 bytes
Ýoʒ›}æʒOQ
Try it online!
This is also correct for 6-bytes, but it doesn't complete in time on TIO for 86:
05AB1E, 6 bytes
ÝoæʒOQ
Try it online!
edited Jan 29 at 15:29
answered Jan 29 at 1:40
Magic Octopus UrnMagic Octopus Urn
12.7k444126
12.7k444126
1
$begingroup$
Both your answers output an empty set for15
, instead of[1,2,4,8]
$endgroup$
– Kevin Cruijssen
Jan 29 at 7:14
1
$begingroup$
@KevinCruijssen needed2**0
, nice catch.Ý
overL
.
$endgroup$
– Magic Octopus Urn
Jan 29 at 15:29
1
$begingroup$
Ah, I know the feeling. Also hadL
instead ofÝ
at first in my answer.
$endgroup$
– Kevin Cruijssen
Jan 29 at 15:31
add a comment |
1
$begingroup$
Both your answers output an empty set for15
, instead of[1,2,4,8]
$endgroup$
– Kevin Cruijssen
Jan 29 at 7:14
1
$begingroup$
@KevinCruijssen needed2**0
, nice catch.Ý
overL
.
$endgroup$
– Magic Octopus Urn
Jan 29 at 15:29
1
$begingroup$
Ah, I know the feeling. Also hadL
instead ofÝ
at first in my answer.
$endgroup$
– Kevin Cruijssen
Jan 29 at 15:31
1
1
$begingroup$
Both your answers output an empty set for
15
, instead of [1,2,4,8]
$endgroup$
– Kevin Cruijssen
Jan 29 at 7:14
$begingroup$
Both your answers output an empty set for
15
, instead of [1,2,4,8]
$endgroup$
– Kevin Cruijssen
Jan 29 at 7:14
1
1
$begingroup$
@KevinCruijssen needed
2**0
, nice catch. Ý
over L
.$endgroup$
– Magic Octopus Urn
Jan 29 at 15:29
$begingroup$
@KevinCruijssen needed
2**0
, nice catch. Ý
over L
.$endgroup$
– Magic Octopus Urn
Jan 29 at 15:29
1
1
$begingroup$
Ah, I know the feeling. Also had
L
instead of Ý
at first in my answer.$endgroup$
– Kevin Cruijssen
Jan 29 at 15:31
$begingroup$
Ah, I know the feeling. Also had
L
instead of Ý
at first in my answer.$endgroup$
– Kevin Cruijssen
Jan 29 at 15:31
add a comment |
$begingroup$
Julia 0.6, 13 bytes
n->n&2.^(0:7)
Try it online!
$endgroup$
add a comment |
$begingroup$
Julia 0.6, 13 bytes
n->n&2.^(0:7)
Try it online!
$endgroup$
add a comment |
$begingroup$
Julia 0.6, 13 bytes
n->n&2.^(0:7)
Try it online!
$endgroup$
Julia 0.6, 13 bytes
n->n&2.^(0:7)
Try it online!
edited Jan 29 at 16:21
answered Jan 29 at 10:01
Kirill L.Kirill L.
5,3431525
5,3431525
add a comment |
add a comment |
$begingroup$
Ruby, 25 bytes
->n{8.times{|i|p n&2**i}}
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 25 bytes
->n{8.times{|i|p n&2**i}}
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 25 bytes
->n{8.times{|i|p n&2**i}}
Try it online!
$endgroup$
Ruby, 25 bytes
->n{8.times{|i|p n&2**i}}
Try it online!
answered Jan 29 at 23:59
iamnotmaynardiamnotmaynard
92349
92349
add a comment |
add a comment |
$begingroup$
CJam, 12 bytes
{:T{2#T&}%}
Try it online!
$endgroup$
add a comment |
$begingroup$
CJam, 12 bytes
{:T{2#T&}%}
Try it online!
$endgroup$
add a comment |
$begingroup$
CJam, 12 bytes
{:T{2#T&}%}
Try it online!
$endgroup$
CJam, 12 bytes
{:T{2#T&}%}
Try it online!
answered Jan 30 at 4:32
Esolanging FruitEsolanging Fruit
8,47432674
8,47432674
add a comment |
add a comment |
$begingroup$
K (oK), 19 16 bytes
-3 bytes thanks to ngn!
{*/x#2}'&|(8#2)
Try it online!
oK does not have power
operator, that's why I need a helper function {*/x#2}
(copy 2 x
times and reduce the resulting list by multiplication)
$endgroup$
$begingroup$
you can omit the{
x}
$endgroup$
– ngn
Jan 31 at 12:49
$begingroup$
@ngn Thanks! I tried it and it worked, but I wasn't sure it is acceptible.
$endgroup$
– Galen Ivanov
Jan 31 at 15:25
add a comment |
$begingroup$
K (oK), 19 16 bytes
-3 bytes thanks to ngn!
{*/x#2}'&|(8#2)
Try it online!
oK does not have power
operator, that's why I need a helper function {*/x#2}
(copy 2 x
times and reduce the resulting list by multiplication)
$endgroup$
$begingroup$
you can omit the{
x}
$endgroup$
– ngn
Jan 31 at 12:49
$begingroup$
@ngn Thanks! I tried it and it worked, but I wasn't sure it is acceptible.
$endgroup$
– Galen Ivanov
Jan 31 at 15:25
add a comment |
$begingroup$
K (oK), 19 16 bytes
-3 bytes thanks to ngn!
{*/x#2}'&|(8#2)
Try it online!
oK does not have power
operator, that's why I need a helper function {*/x#2}
(copy 2 x
times and reduce the resulting list by multiplication)
$endgroup$
K (oK), 19 16 bytes
-3 bytes thanks to ngn!
{*/x#2}'&|(8#2)
Try it online!
oK does not have power
operator, that's why I need a helper function {*/x#2}
(copy 2 x
times and reduce the resulting list by multiplication)
edited Jan 31 at 15:24
answered Jan 29 at 18:55
Galen IvanovGalen Ivanov
7,06211034
7,06211034
$begingroup$
you can omit the{
x}
$endgroup$
– ngn
Jan 31 at 12:49
$begingroup$
@ngn Thanks! I tried it and it worked, but I wasn't sure it is acceptible.
$endgroup$
– Galen Ivanov
Jan 31 at 15:25
add a comment |
$begingroup$
you can omit the{
x}
$endgroup$
– ngn
Jan 31 at 12:49
$begingroup$
@ngn Thanks! I tried it and it worked, but I wasn't sure it is acceptible.
$endgroup$
– Galen Ivanov
Jan 31 at 15:25
$begingroup$
you can omit the
{
x}
$endgroup$
– ngn
Jan 31 at 12:49
$begingroup$
you can omit the
{
x}
$endgroup$
– ngn
Jan 31 at 12:49
$begingroup$
@ngn Thanks! I tried it and it worked, but I wasn't sure it is acceptible.
$endgroup$
– Galen Ivanov
Jan 31 at 15:25
$begingroup$
@ngn Thanks! I tried it and it worked, but I wasn't sure it is acceptible.
$endgroup$
– Galen Ivanov
Jan 31 at 15:25
add a comment |
$begingroup$
TSQL, 43 39 bytes
Can't find a shorter fancy solution, so here is a standard loop.
-4 bytes thanks to MickyT and KirillL
DECLARE @y int=255
,@ int=128s:PRINT @y&@ SET @/=2IF @>0GOTO s
Try it out
When using the query approach, it would take 44 bytes:
use master
DECLARE @ int=87
SELECT @&number
FROM spt_values
WHERE'a'=type
$endgroup$
$begingroup$
using the bitwise and (&) you can save a few with the following,@ int=128s:print @y&@ set @/=2IF @>0GOTO s
. This is hinted by @KirillL for the R answer
$endgroup$
– MickyT
Jan 31 at 19:49
$begingroup$
@MickyT that worked like a charm. Thanks a lot
$endgroup$
– t-clausen.dk
Feb 1 at 0:02
add a comment |
$begingroup$
TSQL, 43 39 bytes
Can't find a shorter fancy solution, so here is a standard loop.
-4 bytes thanks to MickyT and KirillL
DECLARE @y int=255
,@ int=128s:PRINT @y&@ SET @/=2IF @>0GOTO s
Try it out
When using the query approach, it would take 44 bytes:
use master
DECLARE @ int=87
SELECT @&number
FROM spt_values
WHERE'a'=type
$endgroup$
$begingroup$
using the bitwise and (&) you can save a few with the following,@ int=128s:print @y&@ set @/=2IF @>0GOTO s
. This is hinted by @KirillL for the R answer
$endgroup$
– MickyT
Jan 31 at 19:49
$begingroup$
@MickyT that worked like a charm. Thanks a lot
$endgroup$
– t-clausen.dk
Feb 1 at 0:02
add a comment |
$begingroup$
TSQL, 43 39 bytes
Can't find a shorter fancy solution, so here is a standard loop.
-4 bytes thanks to MickyT and KirillL
DECLARE @y int=255
,@ int=128s:PRINT @y&@ SET @/=2IF @>0GOTO s
Try it out
When using the query approach, it would take 44 bytes:
use master
DECLARE @ int=87
SELECT @&number
FROM spt_values
WHERE'a'=type
$endgroup$
TSQL, 43 39 bytes
Can't find a shorter fancy solution, so here is a standard loop.
-4 bytes thanks to MickyT and KirillL
DECLARE @y int=255
,@ int=128s:PRINT @y&@ SET @/=2IF @>0GOTO s
Try it out
When using the query approach, it would take 44 bytes:
use master
DECLARE @ int=87
SELECT @&number
FROM spt_values
WHERE'a'=type
edited Feb 1 at 0:11
answered Jan 29 at 12:12
t-clausen.dkt-clausen.dk
1,934314
1,934314
$begingroup$
using the bitwise and (&) you can save a few with the following,@ int=128s:print @y&@ set @/=2IF @>0GOTO s
. This is hinted by @KirillL for the R answer
$endgroup$
– MickyT
Jan 31 at 19:49
$begingroup$
@MickyT that worked like a charm. Thanks a lot
$endgroup$
– t-clausen.dk
Feb 1 at 0:02
add a comment |
$begingroup$
using the bitwise and (&) you can save a few with the following,@ int=128s:print @y&@ set @/=2IF @>0GOTO s
. This is hinted by @KirillL for the R answer
$endgroup$
– MickyT
Jan 31 at 19:49
$begingroup$
@MickyT that worked like a charm. Thanks a lot
$endgroup$
– t-clausen.dk
Feb 1 at 0:02
$begingroup$
using the bitwise and (&) you can save a few with the following
,@ int=128s:print @y&@ set @/=2IF @>0GOTO s
. This is hinted by @KirillL for the R answer$endgroup$
– MickyT
Jan 31 at 19:49
$begingroup$
using the bitwise and (&) you can save a few with the following
,@ int=128s:print @y&@ set @/=2IF @>0GOTO s
. This is hinted by @KirillL for the R answer$endgroup$
– MickyT
Jan 31 at 19:49
$begingroup$
@MickyT that worked like a charm. Thanks a lot
$endgroup$
– t-clausen.dk
Feb 1 at 0:02
$begingroup$
@MickyT that worked like a charm. Thanks a lot
$endgroup$
– t-clausen.dk
Feb 1 at 0:02
add a comment |
$begingroup$
Alchemist, 125 bytes
_->In_x+128a+m
m+x+a->m+b
m+0x+a->n+a
m+0a->o+Out_b+Out_" "
n+b->n+x+c
n+0b+a->n+c
n+0a->p
o+b->o+c
o+0b->p
p+2c->p+a
p+0c->m
Try it online! or Test every input!
Explanation
_->In_x+128a+m # Initialize universe with input, 128a (value to compare to) and m (state)
m+x+a->m+b # If c has been halved, subtract min(a, x) from a and x and put its value into b
m+0x+a->n+a # If x < a, continue to state n
m+0a->o+Out_b+Out_" " # Else print and continue to state o
n+b->n+x+c # Add min(a, x) (i.e. x) back to x, and add it to c (we're collecting a back into c)
n+0b+a->n+c # Then, add the rest of a to c
n+0a->p # Then, go to state p
o+b->o+c # Add min(a, x) (i.e. a) to c - x _is_ greater than a and so contains it in its binary representation, so we're not adding back to x
o+0b->p # Then, go to state p
p+2c->p+a # Halve c into a
p+0c->m # Then go to state m
$endgroup$
add a comment |
$begingroup$
Alchemist, 125 bytes
_->In_x+128a+m
m+x+a->m+b
m+0x+a->n+a
m+0a->o+Out_b+Out_" "
n+b->n+x+c
n+0b+a->n+c
n+0a->p
o+b->o+c
o+0b->p
p+2c->p+a
p+0c->m
Try it online! or Test every input!
Explanation
_->In_x+128a+m # Initialize universe with input, 128a (value to compare to) and m (state)
m+x+a->m+b # If c has been halved, subtract min(a, x) from a and x and put its value into b
m+0x+a->n+a # If x < a, continue to state n
m+0a->o+Out_b+Out_" " # Else print and continue to state o
n+b->n+x+c # Add min(a, x) (i.e. x) back to x, and add it to c (we're collecting a back into c)
n+0b+a->n+c # Then, add the rest of a to c
n+0a->p # Then, go to state p
o+b->o+c # Add min(a, x) (i.e. a) to c - x _is_ greater than a and so contains it in its binary representation, so we're not adding back to x
o+0b->p # Then, go to state p
p+2c->p+a # Halve c into a
p+0c->m # Then go to state m
$endgroup$
add a comment |
$begingroup$
Alchemist, 125 bytes
_->In_x+128a+m
m+x+a->m+b
m+0x+a->n+a
m+0a->o+Out_b+Out_" "
n+b->n+x+c
n+0b+a->n+c
n+0a->p
o+b->o+c
o+0b->p
p+2c->p+a
p+0c->m
Try it online! or Test every input!
Explanation
_->In_x+128a+m # Initialize universe with input, 128a (value to compare to) and m (state)
m+x+a->m+b # If c has been halved, subtract min(a, x) from a and x and put its value into b
m+0x+a->n+a # If x < a, continue to state n
m+0a->o+Out_b+Out_" " # Else print and continue to state o
n+b->n+x+c # Add min(a, x) (i.e. x) back to x, and add it to c (we're collecting a back into c)
n+0b+a->n+c # Then, add the rest of a to c
n+0a->p # Then, go to state p
o+b->o+c # Add min(a, x) (i.e. a) to c - x _is_ greater than a and so contains it in its binary representation, so we're not adding back to x
o+0b->p # Then, go to state p
p+2c->p+a # Halve c into a
p+0c->m # Then go to state m
$endgroup$
Alchemist, 125 bytes
_->In_x+128a+m
m+x+a->m+b
m+0x+a->n+a
m+0a->o+Out_b+Out_" "
n+b->n+x+c
n+0b+a->n+c
n+0a->p
o+b->o+c
o+0b->p
p+2c->p+a
p+0c->m
Try it online! or Test every input!
Explanation
_->In_x+128a+m # Initialize universe with input, 128a (value to compare to) and m (state)
m+x+a->m+b # If c has been halved, subtract min(a, x) from a and x and put its value into b
m+0x+a->n+a # If x < a, continue to state n
m+0a->o+Out_b+Out_" " # Else print and continue to state o
n+b->n+x+c # Add min(a, x) (i.e. x) back to x, and add it to c (we're collecting a back into c)
n+0b+a->n+c # Then, add the rest of a to c
n+0a->p # Then, go to state p
o+b->o+c # Add min(a, x) (i.e. a) to c - x _is_ greater than a and so contains it in its binary representation, so we're not adding back to x
o+0b->p # Then, go to state p
p+2c->p+a # Halve c into a
p+0c->m # Then go to state m
edited Feb 1 at 1:34
answered Feb 1 at 1:05
ASCII-onlyASCII-only
4,3381238
4,3381238
add a comment |
add a comment |
$begingroup$
Python 2, 43 40 bytes
f=lambda n,p=1:n/p*[1]and f(n,p*2)+[p&n]
Try it online!
$endgroup$
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
Jan 28 at 22:20
1
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
Jan 28 at 22:35
add a comment |
$begingroup$
Python 2, 43 40 bytes
f=lambda n,p=1:n/p*[1]and f(n,p*2)+[p&n]
Try it online!
$endgroup$
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
Jan 28 at 22:20
1
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
Jan 28 at 22:35
add a comment |
$begingroup$
Python 2, 43 40 bytes
f=lambda n,p=1:n/p*[1]and f(n,p*2)+[p&n]
Try it online!
$endgroup$
Python 2, 43 40 bytes
f=lambda n,p=1:n/p*[1]and f(n,p*2)+[p&n]
Try it online!
edited Jan 28 at 22:19
answered Jan 28 at 20:45
ovsovs
19.2k21160
19.2k21160
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
Jan 28 at 22:20
1
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
Jan 28 at 22:35
add a comment |
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
Jan 28 at 22:20
1
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
Jan 28 at 22:35
1
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
Jan 28 at 22:20
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
Jan 28 at 22:20
1
1
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
Jan 28 at 22:35
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
Jan 28 at 22:35
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 33 bytes
n=>{for(;n>0;n&=n-1)Print(n&-n);}
Port of @Arnauld's JavaScript (ES6) answer, so make sure to upvote him!
Try it online.
Explanation:
n=>{ // Method with integer parameter and no return-type
for(;n>0 // Continue looping as long as `n` is larger than 0:
; // After every iteration:
n&=n-1) // Bitwise-AND `n` by `n-1`
Print( // Print with trailing newline:
n&-n);} // `n` bitwise-AND `-n`
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 33 bytes
n=>{for(;n>0;n&=n-1)Print(n&-n);}
Port of @Arnauld's JavaScript (ES6) answer, so make sure to upvote him!
Try it online.
Explanation:
n=>{ // Method with integer parameter and no return-type
for(;n>0 // Continue looping as long as `n` is larger than 0:
; // After every iteration:
n&=n-1) // Bitwise-AND `n` by `n-1`
Print( // Print with trailing newline:
n&-n);} // `n` bitwise-AND `-n`
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 33 bytes
n=>{for(;n>0;n&=n-1)Print(n&-n);}
Port of @Arnauld's JavaScript (ES6) answer, so make sure to upvote him!
Try it online.
Explanation:
n=>{ // Method with integer parameter and no return-type
for(;n>0 // Continue looping as long as `n` is larger than 0:
; // After every iteration:
n&=n-1) // Bitwise-AND `n` by `n-1`
Print( // Print with trailing newline:
n&-n);} // `n` bitwise-AND `-n`
$endgroup$
C# (Visual C# Interactive Compiler), 33 bytes
n=>{for(;n>0;n&=n-1)Print(n&-n);}
Port of @Arnauld's JavaScript (ES6) answer, so make sure to upvote him!
Try it online.
Explanation:
n=>{ // Method with integer parameter and no return-type
for(;n>0 // Continue looping as long as `n` is larger than 0:
; // After every iteration:
n&=n-1) // Bitwise-AND `n` by `n-1`
Print( // Print with trailing newline:
n&-n);} // `n` bitwise-AND `-n`
answered Jan 29 at 9:51
Kevin CruijssenKevin Cruijssen
40k563206
40k563206
add a comment |
add a comment |
$begingroup$
Java 8, 46 bytes
n->{for(;n>0;n&=n-1)System.out.println(n&-n);}
Try it online.
Port of @Arnauld's JavaScript (ES6) answer, so make sure to upvote him!
Explanation:
n->{ // Method with integer parameter and no return-type
for(;n>0 // Continue looping as long as `n` is larger than 0:
; // After every iteration:
n&=n-1) // Bitwise-AND `n` by `n-1`
System.out.println( // Print with trailing newline:
n&-n);} // `n` bitwise-AND `-n`
$endgroup$
$begingroup$
Breakthrough in Java: 36 chars, by using this rule. Let's go back 5 years and golf all Java answers ever produced!
$endgroup$
– Olivier Grégoire
Jan 29 at 16:29
$begingroup$
@OlivierGrégoire Hmm, although I agree that it's most likely allowed, I'll pass tbh. I have used that rule when the input is for example an array, and we modify the input-array instead of returning a new one to save bytes. But adding an additional empty List argument just to store the output in to save byte with.add
instead ofSystem.out.println
seems a bit cheaty imho.
$endgroup$
– Kevin Cruijssen
Jan 29 at 17:13
$begingroup$
Kind of like howArray.sort()
doesn't actually return anything, it just modifies the input. That seems legit. In C# you can usevoid f(out int x)
orvoid f(ref int x)
to return the result, but that doesn't seem to help w/ golf in my experiments.
$endgroup$
– dana
Feb 1 at 9:17
add a comment |
$begingroup$
Java 8, 46 bytes
n->{for(;n>0;n&=n-1)System.out.println(n&-n);}
Try it online.
Port of @Arnauld's JavaScript (ES6) answer, so make sure to upvote him!
Explanation:
n->{ // Method with integer parameter and no return-type
for(;n>0 // Continue looping as long as `n` is larger than 0:
; // After every iteration:
n&=n-1) // Bitwise-AND `n` by `n-1`
System.out.println( // Print with trailing newline:
n&-n);} // `n` bitwise-AND `-n`
$endgroup$
$begingroup$
Breakthrough in Java: 36 chars, by using this rule. Let's go back 5 years and golf all Java answers ever produced!
$endgroup$
– Olivier Grégoire
Jan 29 at 16:29
$begingroup$
@OlivierGrégoire Hmm, although I agree that it's most likely allowed, I'll pass tbh. I have used that rule when the input is for example an array, and we modify the input-array instead of returning a new one to save bytes. But adding an additional empty List argument just to store the output in to save byte with.add
instead ofSystem.out.println
seems a bit cheaty imho.
$endgroup$
– Kevin Cruijssen
Jan 29 at 17:13
$begingroup$
Kind of like howArray.sort()
doesn't actually return anything, it just modifies the input. That seems legit. In C# you can usevoid f(out int x)
orvoid f(ref int x)
to return the result, but that doesn't seem to help w/ golf in my experiments.
$endgroup$
– dana
Feb 1 at 9:17
add a comment |
$begingroup$
Java 8, 46 bytes
n->{for(;n>0;n&=n-1)System.out.println(n&-n);}
Try it online.
Port of @Arnauld's JavaScript (ES6) answer, so make sure to upvote him!
Explanation:
n->{ // Method with integer parameter and no return-type
for(;n>0 // Continue looping as long as `n` is larger than 0:
; // After every iteration:
n&=n-1) // Bitwise-AND `n` by `n-1`
System.out.println( // Print with trailing newline:
n&-n);} // `n` bitwise-AND `-n`
$endgroup$
Java 8, 46 bytes
n->{for(;n>0;n&=n-1)System.out.println(n&-n);}
Try it online.
Port of @Arnauld's JavaScript (ES6) answer, so make sure to upvote him!
Explanation:
n->{ // Method with integer parameter and no return-type
for(;n>0 // Continue looping as long as `n` is larger than 0:
; // After every iteration:
n&=n-1) // Bitwise-AND `n` by `n-1`
System.out.println( // Print with trailing newline:
n&-n);} // `n` bitwise-AND `-n`
answered Jan 29 at 9:54
Kevin CruijssenKevin Cruijssen
40k563206
40k563206
$begingroup$
Breakthrough in Java: 36 chars, by using this rule. Let's go back 5 years and golf all Java answers ever produced!
$endgroup$
– Olivier Grégoire
Jan 29 at 16:29
$begingroup$
@OlivierGrégoire Hmm, although I agree that it's most likely allowed, I'll pass tbh. I have used that rule when the input is for example an array, and we modify the input-array instead of returning a new one to save bytes. But adding an additional empty List argument just to store the output in to save byte with.add
instead ofSystem.out.println
seems a bit cheaty imho.
$endgroup$
– Kevin Cruijssen
Jan 29 at 17:13
$begingroup$
Kind of like howArray.sort()
doesn't actually return anything, it just modifies the input. That seems legit. In C# you can usevoid f(out int x)
orvoid f(ref int x)
to return the result, but that doesn't seem to help w/ golf in my experiments.
$endgroup$
– dana
Feb 1 at 9:17
add a comment |
$begingroup$
Breakthrough in Java: 36 chars, by using this rule. Let's go back 5 years and golf all Java answers ever produced!
$endgroup$
– Olivier Grégoire
Jan 29 at 16:29
$begingroup$
@OlivierGrégoire Hmm, although I agree that it's most likely allowed, I'll pass tbh. I have used that rule when the input is for example an array, and we modify the input-array instead of returning a new one to save bytes. But adding an additional empty List argument just to store the output in to save byte with.add
instead ofSystem.out.println
seems a bit cheaty imho.
$endgroup$
– Kevin Cruijssen
Jan 29 at 17:13
$begingroup$
Kind of like howArray.sort()
doesn't actually return anything, it just modifies the input. That seems legit. In C# you can usevoid f(out int x)
orvoid f(ref int x)
to return the result, but that doesn't seem to help w/ golf in my experiments.
$endgroup$
– dana
Feb 1 at 9:17
$begingroup$
Breakthrough in Java: 36 chars, by using this rule. Let's go back 5 years and golf all Java answers ever produced!
$endgroup$
– Olivier Grégoire
Jan 29 at 16:29
$begingroup$
Breakthrough in Java: 36 chars, by using this rule. Let's go back 5 years and golf all Java answers ever produced!
$endgroup$
– Olivier Grégoire
Jan 29 at 16:29
$begingroup$
@OlivierGrégoire Hmm, although I agree that it's most likely allowed, I'll pass tbh. I have used that rule when the input is for example an array, and we modify the input-array instead of returning a new one to save bytes. But adding an additional empty List argument just to store the output in to save byte with
.add
instead of System.out.println
seems a bit cheaty imho.$endgroup$
– Kevin Cruijssen
Jan 29 at 17:13
$begingroup$
@OlivierGrégoire Hmm, although I agree that it's most likely allowed, I'll pass tbh. I have used that rule when the input is for example an array, and we modify the input-array instead of returning a new one to save bytes. But adding an additional empty List argument just to store the output in to save byte with
.add
instead of System.out.println
seems a bit cheaty imho.$endgroup$
– Kevin Cruijssen
Jan 29 at 17:13
$begingroup$
Kind of like how
Array.sort()
doesn't actually return anything, it just modifies the input. That seems legit. In C# you can use void f(out int x)
or void f(ref int x)
to return the result, but that doesn't seem to help w/ golf in my experiments.$endgroup$
– dana
Feb 1 at 9:17
$begingroup$
Kind of like how
Array.sort()
doesn't actually return anything, it just modifies the input. That seems legit. In C# you can use void f(out int x)
or void f(ref int x)
to return the result, but that doesn't seem to help w/ golf in my experiments.$endgroup$
– dana
Feb 1 at 9:17
add a comment |
1 2
next
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179174%2fsum-of-powers-of-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
Jan 28 at 20:56
2
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
Jan 28 at 20:58
4
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
Jan 28 at 21:38
5
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
Jan 28 at 22:28
5
$begingroup$
I think you might want to state that any power of two may only be used once. Otherwise somebody could output "1 1 1" for the input 3.
$endgroup$
– Black Owl Kai
Jan 29 at 10:58