Quantcast
Channel: Break an integer into even- and odd-indexed bits - Code Golf Stack Exchange
Viewing all articles
Browse latest Browse all 15

Answer by Digital Trauma for Break an integer into even- and odd-indexed bits

$
0
0

C (gcc), 54

TIL 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, but this is as far as I got.

Returns the even bits in the most significant 16 bits and the odd bits in the least significant 16 bits of the returned int.

f(i){i=(_pext_u32(i,~0U/3)<<16)+_pext_u32(i,~0U/3*2);}

Try it online!


Viewing all articles
Browse latest Browse all 15

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>