Conditional Statement using Bitwise operators

后端 未结 2 974
春和景丽
春和景丽 2021-02-04 11:31

So I see that this question has already been asked, however the answers were a little vague and unhelpful. Okay, I need to implement a c expression using only \"& ^ ~ ! + |

2条回答
  •  不思量自难忘°
    2021-02-04 12:14

    I would convert a to a boolean using !!a, to get 0 or 1. x = !!a.

    Then I'd negate that in two's complement. Since you don't have unary minus available, you use the definition of 2's complement negation: invert the bits, then add one: y = ~x + 1. That will give either all bits clear, or all bits set.

    Then I'd and that directly with one variable y & b, its inverse with the other: ~y & c. That will give a 0 for one of the expressions, and the original variable for the other. When we or those together, the zero will have no effect, so we'll get the original variable, unchanged.

提交回复
热议问题