Pregunta

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

¿Fue útil?

Solución

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

Otros consejos

~(~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)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top