How does conditional operator show using Bitwire operators?
EDIT: Sorry for poor explanation. This is a question of homework where I have to use conditioned operator only to use the bitwise operation. It would be simple if the statement is allowed, although it should be strictly a bitwire operator. Takes the function in three ints and acts like a normal conditional operator. This first difference is evaluated and based on the value of the previous one is returned. I was hoping that there would be a simple algorithm for this. Thanksgiving will be a great help to start any idea!
What changes are allowed in the form of bits operators? Are arithmetic operators allowed?
Your edit is not completely clear, but I believe that you need to implement the equivalent of
a? B: c
where a
, b
and c
are integers in return
a! Is equal to = 0? B: C
One way to achieve this is to use non-zero value a
in all bit patterns, using bit-operators. If we understand how to do this, then the rest will be easy. Now, I do not immediately remember some simple tips that they will do (they believe I believe), and I am sure which operators are not allowed And who are not, so now I will only use something like
one. = A & gt; & Gt; 1; One | = A & gt; & Gt; 2; One | = A & gt; & Gt; 4; One | = A & gt; & Gt; 8; One | = A & gt; & Gt; 16; A | = A & lt; & Lt; 1; A | = A & lt; & Lt; 2; A | = A & lt; & Lt; 4; A | = A & lt; & Lt; 8; A | = A & lt; & Lt; 16; For 32-bit integer types, if at least one bit was set (and only if) in the original a
then the result of the above should be the result of all a < Set / code> to 1 bit. (Suppose we are working with an unsigned integer to avoid issues related to transfer of signed values) Then, there must be a more clever method to do this, I am sure. For example: a =! A - 1
, but I do not know if !
and -
are allowed. Once we have done this, the original conditional operator becomes
(A and B). (~ A and C)
is done.
Comments
Post a Comment