You are given three non-negative integers a, b and c. Compute the result of the following compound bitwise expression and return it as an integer:
result = ((a AND b) XOR (b OR c)) AND (NOT (a XOR c))
Here AND is bitwise &, OR is bitwise |, XOR is bitwise ^, and NOT is the bitwise complement restricted to a 32-bit unsigned representation, that is ~x & 0xFFFFFFFF.
The point of the exercise is to trace each bit position through the chain of operations and verify the final value, the way an interviewer might ask you to do it without pen and paper.
Function Signature
def evaluate_expression(a: int, b: int, c: int) -> int:
Rules
-
NOT y
means
~y & 0xFFFFFFFF
, the complement of
y
within 32 bits. AND, OR and XOR act on the plain non-negative values.
-
Return the result as a non-negative integer.
Constraints
-
0 <= a, b, c <= 10^9
-
All inputs fit in a 32-bit non-negative integer.
Examples
Input: a = 12, b = 10, c = 6
Output: 4
In binary, a = 1100, b = 1010 and c = 0110. Then a & b = 1000, b | c = 1110, and their XOR is 0110. Next, a ^ c = 1010, whose 32-bit NOT is ...11110101. The final AND of 0110 and ...0101 is 0100, which is 4.
Input: a = 0, b = 0, c = 0
Output: 0
All operands are 0, so a & b and b | c are both 0, their XOR is 0, and 0 AND anything is 0.
Input: a = 5, b = 3, c = 5
Output: 6
Here a and c are equal, so a ^ c = 0 and its NOT is 0xFFFFFFFF, which keeps every bit of the left side. That side is (101 & 011) ^ (011 | 101) = 001 ^ 111 = 110, which is 6.