2

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?

rakeshbs
  • 24,392
  • 7
  • 73
  • 63
Dark Side
  • 695
  • 2
  • 8
  • 18
  • Array(`int[]`) can't be "always -1"... Would you mind to clarify? – Alexei Levenkov Jan 03 '15 at 07:08
  • You know all the bits in `c`, so the answer is every combination of those bits. But in general it's impossible to do a "reverse" bitwise OR, as if there are more than a single bit set then there are multiple combinations, and there is no single "correct" solution. – Some programmer dude Jan 03 '15 at 07:10
  • By that I mean that v[0] | v[1] = -1 – Dark Side Jan 03 '15 at 07:10
  • Side note: bit masking is done with `&` not with `|`... – Alexei Levenkov Jan 03 '15 at 07:10
  • Does `0, c` works as an answer? Or `c,c`? Do you have additional restrictions on `a` and `b` (like `a & b == 0`)? – Alexei Levenkov Jan 03 '15 at 07:12
  • The only restrictions are: a and b shouldn't be bigger than int.maxvalue and not smaller than int.minvalue. and a | b should be == c . I have no restrictions for &? – Dark Side Jan 03 '15 at 07:20
  • So answer to your task is `0, c` and no additional code is needed... To figure out why your code produces wrong results check out descriptions of corresponding operators and read something on bit-switching - like http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c-c or http://stackoverflow.com/questions/93744/most-common-c-sharp-bitwise-operations-on-enums – Alexei Levenkov Jan 03 '15 at 07:31

2 Answers2

2

You get -1 since you are setting all bits to 1 with OR'ing together number and its negation.

Essential part of your code is:

int a = 42; // you get random number, but it does not matter here.
int b = ~a | c; // "| c" part just possibly add more 1s 

result = a | b; // all 1s as it essentially  (a | ~a) | c  

Proper way of separating bits would be x & ~mask - see links/samples in Most common C# bitwise operations on enums.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
2

Try this code. And operation is used to extract the bits that are same. I don't know C# so I can't type the exact code. The logic is take a random bit mask and AND the bits of c with that bitmask and inverse of that bitmask. The resulting two numbers will be the numbers you need.

int c = 4134;
int a = r.Next(0, value);
int first_num = c & a;
int second_num = c & ~a;
int check_c = first_num | second_num

check_c and c should be equal

rakeshbs
  • 24,392
  • 7
  • 73
  • 63