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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: What does '^' do? Is this a boolean?

    Quote Originally Posted by ColeTrain View Post
    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
    Last edited by Zaphod_b; October 19th, 2012 at 03:00 PM.

Similar Threads

  1. Trying to understand what boolean[][][] is
    By JB4times4 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 29th, 2011, 05:03 PM
  2. [SOLVED] What's wrong with this boolean?
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 8th, 2011, 06:41 PM
  3. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  4. Some help with boolean
    By JPetroSS in forum Java Theory & Questions
    Replies: 3
    Last Post: November 2nd, 2010, 05:38 AM
  5. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 10:15 AM