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 Mukundan314 for Break an integer into even- and odd-indexed bits

$
0
0

C (gcc), 45 bytes

f(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 argument.

Explanation

f(x,                    // recrusive method with integer parameter x  y)int*y;{             // odd bits are returned; even bits are returned through y  *y = x * ~x           //   if x * (x - 1) is non zero (i.e. x is not 0 or -1)      ? x&1 | 2*f(x>>1  //     then y = x&1 | 2*odd_bits(x>>1)        ,&x)            //          x = even_bits(x>>1)      : x;              //     else y = x  x = x;}               //   return x

Viewing all articles
Browse latest Browse all 15

Trending Articles



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