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

$
0
0

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 test-suite.

How?

»~BṚŒœUḄ~>Ƈ¡ - Link: integer, N            e.g.: -42                 42 ~           - bitwise NOT                        41                -43»            - {N} max {that}                     41                 42  B          - convert to binary       [1,0,1,0,0,1]      [1,0,1,0,1,0]Ṛ         - reverse                 [1,0,0,1,0,1]      [0,1,0,1,0,1]Œœ       - unzip               [[1,0,0],[0,1,1]]  [[0,0,0],[1,1,1]]      U      - reverse each        [[0,0,1],[1,1,0]]  [[0,0,0],[1,1,1]]Ḅ     - convert from binary             [1,6]              [0,7]¡ - if...Ƈ  - ...keep those for which:>   -      greater than {N}?          [1,6] (truthy)        [] (falsey)        ~    - ...then: bitwise NOT          [-2,-7]              [0,7]

Viewing all articles
Browse latest Browse all 15

Trending Articles



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