Preparing for exam and got stuck at this question:
Allowed operators are <<,+,& no loops allowed and minimum temp variables.
Write a function in C, that gets 4-bit number (char) and returns mirrored (relative to center) bits.
Example: given b4,b3,b2,b1 return b1,b2,b3,b4
O_o thanks!
it might be not clear, but general language tools are allowed (‘==’,if,>,< etc..)
This is not possible given the constraints of only the operators
<<, +, &and no other constructs besides return.To move b3 from the 3rd position to the 2nd position, you will need a way to shift to the right which requires something like >> or /. Of the operators provided, none can be used with b3 to set the 2nd or 1st bit position.
if you can use if statements and the assignment operator
=, it is possible. You can then write a messy solution such asA more ugly but shorter one liner if you can use the similar to
if?operator.