In this quick tutorial, we'll learn about the Java XOR operator. We'll discuss a bit of theory about XOR operations, and then we'll see how to implement them in Java. Show
2. The XOR OperatorLet's begin with a reminder of the semantics of the XOR operation. The XOR logical operation, exclusive or, takes two boolean operands and returns true if, and only if, the operands are different. Conversely, it returns false if the two operands have the same value. So, for example, the XOR operator can be used when we have to check for two conditions that can't be true at the same time. Let's consider two conditions, A and B. The following table shows the possible values of A XOR B: The A XOR B operation is equivalent to (A AND !B) OR (!A AND B). Parentheses have been included for clarity, but are optional, as the AND operator takes precedence over the OR operator. 3. How to Do It in Java?Now let's see how to express the XOR operation in Java. Of course, we have the option to use the && and || operators, but this can be a bit wordy, as we're going to see. Imagine a Car class having two boolean attributes: diesel and manual. Now let's say we want to tell if the car is either diesel or manual, but not both. Let's check this using the && and || operators:
That's a bit long, especially considering that we have an alternative, the Java XOR operator represented by the ^ symbol. It's a bitwise operator, meaning it's an operator comparing the matching bits of two values in order to return a result. In the XOR case, if two bits of the same position have the same value, the resulting bit will be 0. Otherwise, it'll be 1. So instead of our cumbersome XOR implementation, we can use the ^ operator directly:
As we can see, the ^ operator allows us to be more concise in expressing XOR operations. Finally, it's worth mentioning that the XOR operator, like the other bitwise operators, works with every primitive type. For example, let's consider two integers, 1 and 3, whose binary representations are 00000001 and 000000011, respectively. Using the XOR operator between them will result in the integer 2:
Only the second bit is different in those two numbers; therefore, the result of the XOR operator on this bit will be 1. All other bits are identical, so their bitwise XOR result is 0, giving us a final value of 00000010, the binary representation of the integer 2. 4. ConclusionIn this article, we learned about the Java XOR operator. We demonstrated how it offers a concise way to express XOR operations. Bitwise is a level of operation that involves working with individual bits which are the smallest units of data in a computing system. Each bit has single binary value of 0 or 1. Most programming languages manipulate groups of 8, 16 or 32 bits. These bit multiples are known as bytes. The arithmetic logic unit (ALU) is a part of a computer's CPU. Inside the ALU, mathematical operations like addition, subtraction, multiplication and division are all done at bit level. For those operations, bitwise operators are used. Bitwise operationsA bitwise operation operates on two-bit patterns of equal lengths by positionally matching their individual bits. For example, a logical AND (&) of each bit pair results in a 1 if both the first AND second bits are 1. If only one bit is a 1, the result is 0. AND can also be used to test individual bits in a bit string to see if they are 0 or 1. A logical OR (|) operation functions differently from the AND operations. For each bit pair, the result is 1 if the first OR second bit is 1. If neither bit is 1, the result is 0. A logical XOR (~) of each bit pair results in a 1 if the two bits are different, and 0 if they are the same (both zeros or both ones). Logical NOT is represented as ^. Left shift (<<), right shift (>>) and zero-fill right shift (>>>) bitwise operators are also known as bit shift operators. Arithmetic logic unit, part of a computer CPU, is where bitwise operators used to perform mathematical operations.Bitwise operatorsBitwise operators are characters that represent actions (bitwise operations) to be performed on single bits. They operate at the binary level and perform operations on bit patterns that involve the manipulation of individual bits. Thus, unlike common logical operators like + or - which work with bytes or groups of bytes, bitwise operators can check each individual bit within a byte. The most common bitwise operators used in C/C++ are given in the table below. OperatorNameDescriptionApplication&Bitwise ANDCopies a bit to the result if it exists in both operands. The result is 1 only if both bits are 1.To set up a mask to check the values of specific bits|Bitwise ORCopies a bit to the result if it exists in either operand. The result is 1 if either bit is 1.To add two numbers if there is no carry involved^Bitwise Exclusive OR (XOR)Copies a bit to the result if it exists in either operand. So, if one of the operands is TRUE, the result is TRUE. If neither operand is TRUE, the result is FALSE.To toggle bits or swap two variables without using a third temporary variableTo find specific types of numbers (e.g., odd) in a series of numbers (e.g., all even) To find nonrepeating elements To detect if two integers have opposite signs~Bitwise NOTAlso known as bitwise complement and bitwise inversion, it flips zeros into ones and ones into zeros.To flip or invert bits<<Shift leftThe left operand value is shifted left by the number of bits specified by the right operand.To align bits>>Shift rightThe left operand value is shifted right by the number of bits specified by the right operand.To align bitsMultiple bitwise operators are used in bit manipulation. These operations happen very fast and optimize system performance and time complexity. It's important to keep in mind that the left shift and right shift operators should not be used for negative numbers. Doing this can result in undefined behaviors in the programming language. Also, bitwise operators should not be used in place of logical operators because they work differently. Logical operators consider non-zero operands as 1 and their result is either 0 or 1. In contrast, bitwise operators return an integer value. The table below defines the JavaScript bitwise operators. OperatorNameTypeAction&Bitwise ANDBinaryIf bits of both operands are ones, returns a one in each bit position|Bitwise ORBinaryIf bits of either operand are ones, returns a one in a bit position^Bitwise XORBinaryIf a single operand is a one, returns a one in a bit position~Bitwise NOTUnaryFlips the bits in the operand<<Left shiftBinaryShifts first operand a number of bits to the left as specified in the second operand, shifting in zeros from the right>>Right shiftBinaryShifts first operand a number of bits to the right as specified in the second operand, and discards displaced bits>>>Zero-fill right shiftBinaryShifts first operand a number of bits to the right as specified in the second operand, discards displaced bits, and shifts in zeros from the leftApplications of bitwise operations and operatorsThere are many applications of bitwise operations and operators. For one, they are used in data compression where data is converted from one representation to another to reduce the amount of storage space required. Bitwise operations are also used in encryption algorithms to encrypt data and protect it from unauthorized use, manipulation or exfiltration. The following are some other common applications:
Bitwise ANDThe bitwise AND operator produces an output of 1 if the corresponding bits of both the operands are 1. If not, the output is 0. Example 1: Bitwise AND operation of two one-bit operands. Left operandRight operandResult000010100111Example 2: Bitwise AND operation of two integers: 28 and 17; the & operator compares each binary digit of these integers. Binary digits28000111001700010001Are both digits 1?NoNoNoYesNoNoNoNoBitwise AND output00010000Thus: 28 & 17 (bitwise AND) = 00010000 (binary) = 16 (decimal). Bitwise ORThe bitwise OR operator produces an output of 1 if either one of the corresponding bits is 1. Otherwise, the output is zero. Example 1: The bitwise OR operation of two one-bit operands. Left operandRight operandResult000011101111Example 2: Let's consider the previous example of two integers: 28 and 17. Binary digits28000111001700010001Is either digit 1?NoNoNoYesYesYesNoYesBitwise OR output00011101Thus: 28 | 17 (bitwise OR) = 00011101 (binary) = 29 (decimal). Bitwise exclusive OR (XOR)The bitwise exclusive OR (XOR) operator returns 1 if the bits of both operands are opposite. Otherwise, it returns 0. Example 1: The bitwise XOR operation of two one-bit operands. Left operandRight operandResult000011101110Example 2: Let's see how bitwise XOR works for our two integers 28 and 17. Binary digits28000111001700010001Are the two digits opposite of each other?NoNoNoNoYesYesNoYesBitwise XOR output00001101Thus: 28 ^ 17 (bitwise XOR) = 00001101 (binary) = 13 (decimal). Bitwise NOTThe bitwise NOT operator reverses the bits. Unlike other bitwise operators, it accepts only one operand. Example: Let's consider the bitwise NOT operation of the integer 28. Binary digits2800011100Bitwise NOT output11100011Thus: ~28 (bitwise NOT) = 11100011 (binary) = 227 (decimal). Bitwise left shiftThe bitwise left shift operator shifts the bits left by the bits specified by the right operand. The positions vacated by the left shift operator are filled with 0. Example: Let's perform the bitwise left shift operation on the integer 6. Each bit will be shifted left by 1. 6 = 0110 6<<1 = 1100 (binary) = 12 (decimal) Bitwise right shiftLike the left shift operator, the bitwise right shift operator shifts the bits right by the bits specified by the right operand. The positions vacated by the right shift operator are filled with 0. Example: Let's perform the right shift by two bits operations on the integer 8. Each bit will be shifted right by 2. 8 = 1000 8>>2 = 0010 (binary) = 2 (decimal) See also: bit stuffing, logic gate (AND, OR, XOR, NOT, NAND, NOR and XNOR), How many bytes for..., classical computing, Advanced Business Application Programming This was last updated in July 2022 Continue Reading About bitwise
Related TermshotfixA hotfix is code -- sometimes called a patch -- that fixes a bug in a product. See complete definitionKotlinKotlin is a statically typed, object-oriented programing language that is interoperable with the Java virtual machine, Java Class... See complete definitionperformance testingPerformance testing is a testing measure that evaluates the speed, responsiveness and stability of a computer, network, software ... See complete definitionWord of the Day adversarial MLAdversarial machine learning is a technique used in machine learning to fool or misguide a model with malicious input. What is the output of Exclusive OR operator if one of the operands is true?Exclusive or (XOR, EOR or EXOR) is a logical operator which results true when either of the operands are true (one is true and the other one is false) but both are not true and both are not false. In logical condition making, the simple "or" is a bit ambiguous when both operands are true.
What is the output of an Exclusive OR () operation if one of the operands is false?4) What is the output of an Exclusive OR(^) operation, if one of the operands is false? Explanation: Exclusive OR (^) gives an output of true if both the operands are different. If both are the same (true / false), the output is false.
What is the output of a logical OR operation if one of the inputs operands is true?The logical AND operation will yield a TRUE only if all the operands are TRUE. Table 26.4 gives the result of the AND (&&) operator for the operation A&& B. The logical OR operation yields a TRUE if any one of the operands is TRUE.
Which operator returns true if one of the operands is true?Logical Operators
True if either operand is true.
|