I have a problem. I am trying to find a combination of right shift operations. An example. I have an Input:
Let's say 30. I now what to find a and b, so that a >> b = mynumber (30).
That's my attempt:
private static Random rnd = new Random();
private static int[] FindRightShift(int value)
{
int[] arr = new int[2];
for (int i = 1; i < int.MaxValue; i++)
{
int b;
if (int.TryParse((Math.Log(i / value) / Math.Log(2)).ToString(), out b))
{
arr[0] = i;
arr[1] = b;
Console.WriteLine("{0} >> {1} = {2}", arr[0], arr[1], arr[0] >> arr[1]);
// return arr;
}
}
return arr;
}
This works but it takes ages since it is looping trough all combinations. How can I modify my function so it will return one random combination (of a and b) without looping through all?