calgorithm

Bitwise operation without using any predefined C functions

I need to write a program in C to solve the following problem but the catch is that i cannot use loops, conditions, or any predefined C functions

You are given three numbers a∣b, a∣c, and b∣c, where ∣ represents the logical operator or. For example, 3∣5=011∣101=111=7. You will be prompted to print the three numbers a, b and c in the output.

Obviously, this issue does not necessarily have a unique answer and you only need to output one of the valid answers.

Example:

``````input: 5 7 7
output: 1 4 7
``````

I tried to turn the decimal input numbers into binary but couldn't do it without using a loop.

Solution

• Look at this problem one bit at a time. Define `d = a | b`, `e = a | c`, `f = b | c`.

• If all three of {`d`, `e`, `f`} are 0, then we know {`a`, `b`, `c`} are all 0 too.

• If exactly two of {`d`, `e`, `f`} are 0, that is impossible because each variable {`a`, `b`, `c`} will force two of {`d`, `e`, `f`} to be 1.

• If exactly one of {`d`, `e`, `f`} is 0, then exactly one of {`a`, `b`, `c`} is 1, and the rest are 0.

• If exactly none of {`d`, `e`, `f`} are 0, then it's ambiguous at any two or three of {`a`, `b`, `c`} can be 1.

It suffices to solve: `a = d & e`, `b = d & f`, `c = e & f`.