Split, flip and recombine integers
Jelly, 17 16 15 bytes
BUi1µ2*³:2*×Ḥ’$
Try it online!
How it works
BUi1µ2*³:2*×Ḥ’$ Main link. Input: n
B Convert n to base 2.
U Reverse the array of binary digits.
i1 Get the first index (1-based) of 1.
This yields a + 1.
µ Begin a new, monadic chain. Argument: a + 1
2* Compute 2 ** (a+1).
³: Divide n (input) by 2 ** (a+1).
: performs integer division, so this yields b.
2* Compute 2 ** b.
$ Combine the two preceding atoms.
Ḥ Double; yield 2a + 2.
’ Decrement to yield 2a + 1.
× Fork; multiply the results to the left and to the right.
Pyth, 16 15 bytes
*hyJ/PQ2^2.>QhJ
1 byte thanks to Dennis
Test suite
Explanation:
*hyJ/PQ2^2.>QhJ
Implicit: Q = eval(input())
PQ Take the prime factorization of Q.
/ 2 Count how many 2s appear. This is a.
J Save it to J.
y Double.
h +1.
.>QhJ Shift Q right by J + 1, giving b.
^2 Compute 2 ** b.
* Multiply the above together, and print implicitly.
Python 2, 39 bytes
lambda n:2*len(bin(n&-n))-5<<n/2/(n&-n)
n & -n
gives the largest power of 2 that divides n
. It works because in two's-complement arithmetic, -n == ~n + 1
. If n
has k trailing zeros, taking its complement will cause it to have k trailing ones. Then adding 1 will change all the trailing ones to zeroes, and change the 2^k bit from 0 to 1. So -n
ends with a 1 followed by k 0's (just like n
), while having the opposite bit from n
in all higher places.