\$\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 AllanJan 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\$
– SpookyGengarJan 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 KaiJan 29 at 10:58

JavaScript (ES6), 28 bytes

\$\begingroup\$You're the only person in the whole world who can make me upvote JavaScript answers!\$\endgroup\$
– sergiolJan 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\$
– ShaggyJan 29 at 13:23

\$\begingroup\$@Shaggy Because Arnauld seems the only person to do such Javascript solutions. His answers are pure genius!\$\endgroup\$
– sergiolJan 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\$
– ArnauldJan 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\$
– ArnauldJan 29 at 19:47

"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_i\in\{0,1\},\:\forall\:\: i\in\overline{1,n}\$, such that:
$$X=\sum_{i=1}^n x_i\cdot 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.

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.

\$\begingroup\$... what?! Never honestly seen bitwise and used in osabie. Nice one.\$\endgroup\$
– Magic Octopus UrnJan 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 CruijssenJan 29 at 15:55

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)

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)

\$\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 IgnoranceJan 29 at 16:34

\$\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 CordesJan 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 CordesJan 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 CordesJan 30 at 4:11

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

\$\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\$
– MickyTJan 31 at 19:49

\$\begingroup\$@MickyT that worked like a charm. Thanks a lot\$\endgroup\$
– t-clausen.dkFeb 1 at 0:02

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

\$\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égoireJan 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 CruijssenJan 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\$
– danaFeb 1 at 9:17

Your Answer

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