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

$
0
0

Charcoal, 26 bytes

Nθ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 convert from one's complement to two's complement. Also, the array has its most significant bit first and I don't have an easy way to slice alternate bits depending on the index of the last bit.

Nθ                          First input as a number²                      Literal integer `2`E                       Map over implicit rangeθ              Input number⁺               Plus⁰            Literal integer `0`›             Is greater thanθ           Input number↨                Converted to base²          Literal integer `2`⮌                 ReversedΦ                  Filtered whereι        Outer value⁼         Equalsμ      Inner index﹪       Modulo²     Literal integer `2`⮌                   Reversed↨                     Converted from base²                    Literal integer `2`⁻                        Vectorised substract⁰   Literal integer `0`›    Is greater thanθ  Input numberI                         Cast to string                            Implicitly print

Viewing all articles
Browse latest Browse all 15

Trending Articles