Answer by Mukundan314 for Break an integer into even- and odd-indexed bits
Python, 55 bytesPort of @l4m2's answer-1 byte thanks to @l4m2f=lambda x:x+x%2and[x%2+f(x//4)[0]*2,f(x//2)[0]]or[x,x]Attempt This Online!
View ArticleAnswer by l4m2 for Break an integer into even- and odd-indexed bits
JavaScript (Node.js), 47 bytes, \$O\left(n^{\log_2\frac{1+\sqrt 5}2}\right)\$f=n=>n+1>>1?[f(n>>2)[0]*2|n&1,f(n>>1)[0]]:[n,n]Try it online!JavaScript (Node.js), 48...
View ArticleAnswer by Julian for Break an integer into even- and odd-indexed bits
PowerShell Core, 130 bytes($b=[Convert]::ToString(+"$args",2)|% t*y)|%{$c=$b[-++$i]if($i%2){$e="$c$e"}else{$o="$c$o"}}$e,$o|%{[Convert]::ToInt16("0$_",2)}Try it online!Takes an integer and returns two...
View ArticleAnswer by Neil for Break an integer into even- and odd-indexed bits
Charcoal, 26 bytesNθI⁻E²↨²⮌Φ⮌↨⁺θ›⁰θ²⁼ι﹪μ²›⁰θTry it online! Link is to verbose version of code. Explanation: Charcoal's base conversion simply negates the result if the input is negated, so I have to...
View ArticleAnswer by lyxal for Break an integer into even- and odd-indexed bits
Vyxal 3, 12 bytes„¿⌐bU;ᵛB?„¿⌐Try it Online! (link is to literate version)Could be 5 bytes if it wasn't for the negative input requirement.Explainednegative? if-top: bitwise-not ## „¿⌐to-binary...
View ArticleBreak an integer into even- and odd-indexed bits
ObjectiveGiven an integer \$n\$ interpreted as two's complement binary, output two integers, namely the integer consisting of \$n\$'s bits at places of \$2^0, 2^2, 2^4, \cdots\$, and the integer...
View ArticleAnswer by Digital Trauma for Break an integer into even- and odd-indexed bits
C (gcc), 54TIL modern x86-64 has instructions for this, and they are wrapped in the _pext_*() family of compiler intrinsics.Not the shortest, but perhaps the fastest. Probably this can be golfed more,...
View ArticleAnswer by Mukundan314 for Break an integer into even- and odd-indexed bits
C (gcc), 45 bytesf(x,y)int*y;{*y=x*~x?x&1|2*f(x>>1,&x):x;x=x;}Try it online!Odd bits are returned directly, while even bits are returned through the pointer in the second...
View ArticleAnswer by Jonathan Allan for Break an integer into even- and odd-indexed bits
Jelly, 12 bytes»~BṚŒœUḄ~>Ƈ¡A monadic Link that accepts an integer, \$n\$, and yields a pair of non-negative integers, \$e,o\$ ([even_bits_as_int(n), odd_bits_as_int(n)].Try it online! Or see the...
View ArticleAnswer by ovs for Break an integer into even- and odd-indexed bits
K (ngn/k), 22 bytesReturns odd output before even output.2/64 2#,/|@\^:\(64#2)\Try it online!(64#2)\ Get an array of all bits of the integer.,/|@\^:\ Magic incantation to repeat the leading bit 65...
View ArticleAnswer by Kevin Cruijssen for Break an integer into even- and odd-indexed bits
Java, 79 bytesint[]f(int n){return new int[]{n*~n<0?f(n>>2)[0]*2|n%2:n,n*~n<0?f(n>>1)[0]:n};}It's been a while since I've used a recursive approach in Java. Perhaps an iterative...
View ArticleAnswer by Kevin Cruijssen for Break an integer into even- and odd-indexed bits
05AB1E, 15 14 bytes±‚àbR2ιíCI0‹i±Try it online or verify all test cases.Explanation:± # Bitwise-NOT (-n-1) the (implicit) input‚ # Pair it with the (implicit) inputà # Pop and leave the maximum b #...
View ArticleAnswer by corvus_192 for Break an integer into even- and odd-indexed bits
x86 machine code, 18 bytes080497c8 <f>: 80497c8: be 55 55 55 55 mov esi,0x55555555 80497cd: c4 e2 7a f5 d6 pext edx,eax,esi 80497d2: f7 d6 not esi 80497d4: c4 e2 7a f5 c6 pext eax,eax,esi...
View ArticleAnswer by RomanPro100 for Break an integer into even- and odd-indexed bits
Uiua 0.10.0, 22 bytes↙2▽2⍜⌵(⊕°⋯◿2⇡⧻.⊂⋯):<0.Explanation + See it in action
View ArticleAnswer by Quadruplay for Break an integer into even- and odd-indexed bits
Easyfuck, 99 bytess(}}}})a(>{>{<<}`(>>+<<)}`(>+<))$,.^$/~++>$"J*[>~+<;]>aaaa>Y>YJ[<~s+<~s+;]J$-`(<s<s)JR!.<'2.!.<'@--Just barely got it...
View Article