Let's say I have an 15 as c. c can be splitted into a = 7 and b = 13. That's because a | b = c. I am trying to write a function that will find one possible combination of a and b by only giving c as input.
class Program
{
static void Main(string[] args)
{
int data = 560;
int[] v = FindBitshiftOr(data);
Console.WriteLine("{0} << {1} = {2}", v[0], v[1], v[0] | v[1]);
Console.ReadKey();
}
private static Random r = new Random();
private static int[] FindBitshiftOr(int value)
{
int[] d = new int[2];
int a = r.Next(0, value);
int c = r.Next(0, value);
int b = ~a;
d[0] = a;
d[1] = b | c;
return d;
}
}
This was my attempt but its always returning -1, can someone explain me what's wrong?