Computer Science, asked by RAHULSEN504, 1 year ago

what is the difference between bitwise AND (&) and bitwise OR (|) in Java?

Answers

Answered by kvnmurty
6
Bitwise operators in Java are:

   AND:  &  : 
       When we want to AND bitwise the two operands or integers (of same size) we use this operator.   Suppose A = 25.   or 0x19.     Let B = 0x14 = 20.

     A & B = 0001 1001   &  0001 0100   =  0001 0000  = 16

            nth bit of A is ANDed with bit n of B.

     1 & 1 = 1 ;     1 & 0 = 0 ;    0  &  1 = 0  ;     0 & 0 = 0.



 OR :  | 

     When we want bitwise OR of two operands or integers we use this operator.  The bit 1 of A is ORed with bit 1 of B. Bit n of A is ORed with bit n of B.
 
        A | B = 0001 1101  = 29

       1 | 1 = 1 ;    1 | 0 = 1  ;     0 | 1 = 1;       0 | 0 = 0.

kvnmurty: :-)
RAHULSEN504: i need one more point
kvnmurty: I think there is a difference in the associativity and priority.
kvnmurty: if in an expression & and | are both present then & has higher priority. It's done first before |
kvnmurty: Another difference could be associativity. If multiple & operators are there like a & b & c & d, then which two are ANDed first. From left side or right side. ..
kvnmurty: Similarly for OR, if a | b | c | d is given, which two operands are evaluated first. This is called Associativity
kvnmurty: If & and | are there like a & b | c | d & e , then which operator is done first? This is called Precedence. & is done first. & has higher precedence.
Similar questions