# What does '^' do? Is this a boolean?

Printable View

• October 19th, 2012, 12:31 PM
ColeTrain
What does '^' do? Is this a boolean?
I was making a program that told you whether or not the integer you entered was divisible by 5 and 6, 5 or 6, 5 or 6 but not both. The first 2 worked fine with the way I did it and displayed true or false, but I think there is an easier way. The last one did not work right at all. Then I looked at the solution and used the code for the last one, but I dont understand what it did.

Here's the code:
Code java:

```import java.util.Scanner;   public class Pg127 {   public static void main(String[] args) {   //Input and output= Console   java.util.Scanner input = new java.util.Scanner(System.in);   System.out.println("Enter an interger: ");   int integer = input.nextInt();     if (integer % 5 == 0 && integer % 6 == 0) { System.out.println("Is " + integer + " divisible by 5 and 6? true"); } else { System.out.println("Is " + integer + " divisible by 5 and 6? false"); }     if (integer % 5 == 0 || integer % 6 == 0) { System.out.println("Is " + integer + " divisible by 5 or 6? true "); } else { System.out.println("Is " + integer + " divisible by 5 or 6? false"); }     //here is the one I don't understand: System.out.println("Is " + integer + " divisible by 5 or 6, but not both? " + ((integer % 5 == 0) ^ (integer % 6 == 0)));         } }```
What did this do? What does ^ do? Did it somehow become a Boolean?
• October 19th, 2012, 01:05 PM
helloworld922
Re: What does '^' do? Is this a boolean?
^ is the Java Bitwise XOR (exclusive-or) operator.

It doesn't have be operation on only booleans and the result isn't always a boolean, but it can be.
• October 19th, 2012, 02:58 PM
Zaphod_b
Re: What does '^' do? Is this a boolean?
Quote:

Originally Posted by ColeTrain
What did this do? What does ^ do? Did it somehow become a Boolean?

From the Java Language Specification, Third Edition:
Quote:

15.22.1 Integer Bitwise Operators &, ^, and |
When both operands of an operator &, ^, or | are of a type that is convertible
(§5.1.8) to a primitive integral type, binary numeric promotion is first performed
on the operands (§5.6.2). The type of the bitwise operator expression is the promoted
type of the operands.
For &, the result value is the bitwise AND of the operand values.
For ^, the result value is the bitwise exclusive OR of the operand values.
For |, the result value is the bitwise inclusive OR of the operand values.
For example, the result of the expression 0xff00 & 0xf0f0 is 0xf000. The
result of 0xff00 ^ 0xf0f0 is 0x0ff0.The result of 0xff00 | 0xf0f0 is 0xfff0.

15.22.2 Boolean Logical Operators &, ^, and |
When both operands of a &, ^, or | operator are of type boolean or Boolean, then
the type of the bitwise operator expression is boolean. In all cases, the operands
are subject to unboxing conversion (§5.1.8) as necessary.
For &, the result value is true if both operand values are true; otherwise, the
result is false.
For ^, the result value is true if the operand values are different; otherwise,
the result is false.
For |, the result value is false if both operand values are false; otherwise,
the result is true.
Bottom lines:
1. '^' operating on two Java integer data types, (or on expressions that evaluate to integer data types) gives an integer that is the bit-by-bit result of the Boolean Algebra exclusive or logic operation.

2. '^' operating on two Java boolean data types (or expressions that evaluate to boolean data types) gives a boolean result according to the definition of the Boolean Algebra exclusive-or operation.

Cheers!

Z