Question

I need to make a function using just ~ and |, such that function f(6, 5) will return 4 as the answer.

Was it helpful?

Solution

By De Morgan's law, ~(a | b) equals ~a & ~b. By the same law, a & b equals ~(~a | ~b).

OTHER TIPS

~(~a|~b)

Interpretation:

~a : bits which are 0 in a

~b : bits which are 0 in b

~a|~b: bits which are 0 in either a or b

~ of ~a|~b: bits which are not 0 in either a or b, that is they are 1 in both

int f(int a, int b)
{
    return ~(~a|~b);
}

De Morgan's Theorem

Check out the

http://en.wikipedia.org/wiki/De_Morgan%27s_laws

so the below line will help you to achieve adding using negation and bitwise OR

~(~a | ~b)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top