1. ## undo bitshift

Is it possible to find possible parameters for a bitshift operation?

that is, for
Code:
```int literal = 0xf0;
int result = literal << variable;```
can I apply some bitwise operation to find a value for "variable" given a result and literal.

It's easy enough to do in my head (just count the least significant digits until I get a 1), but what's the best way to do this with code?

2. i dont know how u can do it by using bitwise opeartor but i just want to say that left shift is the same as multiplying "literal" by 2 raised to the power "variable". So u can divide "result" by "literal" and take its square root to get the value of "variable".

3. sounds right but when I try it, it doesn't work:

Code:
```#include <math.h>
#include <stdio.h>

int main(int argc, char **argv) {

int variable = 3;
int literal = 0xf0;
int result = literal << variable;
int rl = result/literal;
printf("literal: %x\n",literal);
printf("result: %x\n",result);
printf("result/literal %x \n",rl);
printf("sqrt(result/literal): %x\n",sqrt(rl));

}```
gives me

Code:
```\$./test
literal: f0
result: 780
result/literal 8
sqrt(result/literal): 667f3bcd```
have I missed something?

4. sqrt is not the right thing to use here, I'm pretty sure. Also, it returns a floating point value, so using %x is definitely wrong (unless you actually want to see what the floating point value looks like as a integer - but that's unlikely).

I guess you could do:
Code:
`x = (int)(log(result / literal) / log(2));`
But it's probably quicker to do:
Code:
```int c = 0;
int x = result / literal;
while (x) {  c++; x >>= 1; }```
c is the number of bits literal was shifted.

--
Mats

5. ok, so i guess
Code:
` variable = c-1;`
since we don't want count the last bit from >>= operation, which would be the first 1.

thanks everyone!

6. Ok, yes, or use while(x > 1).

--
Mats

7. Originally Posted by young turk
sounds right but when I try it, it doesn't work:

Code:
```#include <math.h>
#include <stdio.h>

int main(int argc, char **argv) {

int variable = 3;
int literal = 0xf0;
int result = literal << variable;
int rl = result/literal;
printf("literal: %x\n",literal);
printf("result: %x\n",result);
printf("result/literal %x \n",rl);
printf("sqrt(result/literal): %x\n",sqrt(rl));

}```
gives me

Code:
```\$./test
literal: f0
result: 780
result/literal 8
sqrt(result/literal): 667f3bcd```
have I missed something?
sorry i was wrong with the square root part. But going in the similar manner and taking the log(with base 2) of 'rl' will give the value of 'variable'.
I have modified your code by taking the above mentioned into consideration and changed various values of 'variable' and the result was satisfactory.
Code:
```#include <math.h>
#include <stdio.h>

int main(void) {

int variable = 5;//changed it to 2,3,4 etc.
int literal = 4;
int result = literal << variable;
float rl = (float)result/literal;
printf("literal: %d\n",literal);
printf("result: %d\n",result);
printf("result/literal %f \n",rl);
printf("variable=sqrt(result/literal): %f\n",log(rl)/log(2));//log(with base 2) of rl. In C log with a base of 10 is given so i changed it accordingly
getch();
return 0;

}```