By De Morgan's law, ~(a | b)
equals ~a & ~b
. By the same law, a & b
equals ~(~a | ~b)
.
How to do bitwise ANDING in c using just ~ and |
-
14-04-2022 - |
Domanda
I need to make a function using just ~
and |
,
such that function f(6, 5)
will return 4
as the answer.
Soluzione
Altri suggerimenti
~(~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);
}
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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow