Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
x & y =R where '&' is a bitwise AND operator
then how do I get a value x using values R and y?
Posted
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 3:20am    
Is it interview question? Very elementary, but makes sense to check up basic understanding of things. You really did not know the answer, did you? Because you don't need any knowledge, just a pure logic.
--SA
Shreyajit 3-Aug-11 5:08am    
No I do have the basic knowledge. What I want that is there any solution for this kind of problems so that I can get my operand values back from the result value with the help of bitwise operators
CPallini 3-Aug-11 3:24am    
I suppose you should be persuaded, at least on statistical grounds, that bitwise and is a lossy operation. :-D

It is not possible. For example, consider the following case:

x=0110 //will be unknown
&
Y=0101
=====
R=0100


You can restore all bits except bits #1 and #3, where both the bit of known operand and a bit of result are both 0. In this case, the bit of the first (unknown) operand can be either 0 or 1, it would not affect the result bit. In this example, x can be 1110, 0110, 1100 or 0100.

—SA
 
Share this answer
 
v2
You can't - AND "throws away" information.
If you have the binary values 101 and 110, and you AND them together, you get the binary result 100. from that result you can say that both X and Y are members of the set { 100, 101, 110, 111 } - if there are only three bits involved - but you cannot say which value was assigned to which variable.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 3:17am    
Too late: I was the first with the same answer :-). Same time, in fact, my 5.
--SA
Generally speaking you cannot.
Suppose, for instance, y=0, then R=0 regardless of the value of x.
On the other hand, if all bits of y are set then you have full info about x, since x=R.
In other words:
  • if a bit of y is set (equal to 1) then corrensponding bit of x is equal to the corresponding bit of R.
  • if a bit of y is reset (equal to 0) then R provides no useful info and the corrensponding bit of x may be either 1 or 0.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 3:31am    
Clear explanation, my 5.
--SA
CPallini 3-Aug-11 3:39am    
Thanks, you already had my five too. :-)
The XOR operation is reversible.

If R = x ^ y, then x = y ^ R.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 3:30am    
Perfectly true, so what? It does not answer the question. Addition and subtraction are also reversible...
(I did not vote.)
--SA
YvesDaoust 5-Aug-11 3:34am    
Just answering the question that's behind the question.
With '&' operator, you cannot get the x value.

Suppose x=5 and y=4, then r=4

Behind the scenes it would done like so :

x= 0101
y= 0100
   ----&
r= 0100 


And if we have x=5 and y=6, then we also get r=4

Behind the scenes it would done like so :

x= 0101
y= 0110
   ----&
r= 0100


You see, 5 & 4 and 5 & 6 have the same result.

So, how would you get the x value ? The answer is never. Because '&' operator does the bitwise operation, not arithmetic operation.
 
Share this answer
 
v2
Comments
Dalek Dave 3-Aug-11 3:39am    
Nice.
Shreyajit 3-Aug-11 5:11am    
thanks Agus Syahputra, I got it now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900