Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

1. ## 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:
```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?

2. ## 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.

3. ## Re: What does '^' do? Is this a boolean?

Originally Posted by ColeTrain
What did this do? What does ^ do? Did it somehow become a Boolean?
From the Java Language Specification, Third Edition:
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