The following describes a function which I want to solve mathematically rather than resorting to binary bit manipulation is possible:
$y = f(x)$
where
- $x$ is an arbitrary integer equal to or greater than $zero$. $Zero$ can be omitted if that helps.
- The function $f(x)$ sets all binary bits of $x$ to 0 except the two most significant (high order) bits that are set. Example: $f(10101100)=10100000$ and $f(01111111)=01100000$.
- I already have the $base 2 log$ calculated for $x$.
Using this pre-calculated $log$ or some other method, can we calculate $y$ mathematically without resorting to bit manipulation?
I'm looking for an alternative since bit manipulation for arbitrarily large numbers can be quite inefficient depending on what software platform and language you use.
PS: I'm not even sure what keywords to tag this question with.
If you want to maintain the highest two 1 bits in a binary number, the code I gave in your previous question will work:
m=int(log_2(x)): finds the highest 1 bit
y=x-2^m: strips off that 1 bit
n=int(log_2(y)): finds the second highest 1 bit
f(x)=2^m+2^n
This is different from saving the two highest bits as most (including Brian M. Scott) would think the two highest bits of 10001000 were the leftmost 10, not the 2^7 bit and the 2^3 bit.