Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between bitwise inclusive or and exclusive or in java

public class Operators {

    public static void main(String[] args) {        
        int a = 12;

    System.out.println("Bitwise AND:"+(12&12));
    System.out.println("Bitwise inclusive OR:"+(12|12));
    System.out.println("Bitwise exclusive OR:"+(12^12));

    }
}

OUTPUT:

Bitwise AND:12
Bitwise inclusive OR:12
Bitwise exclusive OR:0

I understand first two, but not the third.

like image 723
PSR Avatar asked May 20 '13 04:05

PSR


People also ask

What is the difference between bitwise OR and bitwise exclusive OR?

The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1. The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

What is bitwise exclusive OR and inclusive OR?

BITWISE INCLUSIVE OR (|) means normal or operation , BITWISEE ExCLUSIVE OR (^) means xor operation.

What is bitwise inclusive OR in Java?

The | (bitwise inclusive OR) operator compares the values (in binary format) of each operand and yields a value whose bit pattern shows which bits in either of the operands has the value 1 . If both of the bits are 0 , the result of that bit is 0 ; otherwise, the result is 1 .

Is XOR the same as bitwise OR?

XOR is a bitwise operator, and it stands for "exclusive or." It performs logical operation. If input bits are the same, then the output will be false(0) else true(1).


2 Answers

XOR tells whether each bit is different.

1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0

In other words "either but not both"

0011 XOR 0101 = 0110

like image 65
Mel Nicholson Avatar answered Sep 19 '22 04:09

Mel Nicholson


BITWISE INCLUSIVE OR (|) means normal or operation ,

BITWISEE ExCLUSIVE OR (^) means xor operation

like image 27
Kelum Avatar answered Sep 19 '22 04:09

Kelum