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 6 of 6

Thread: how operator( && or || works).....

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how operator( && or || works).....

    int k=5|6|7; how it works ....
    int k=4&5&6; how it work .....


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: how operator( && or || works).....

    it's bit-wise operation: OR | and the other AND &

  3. #3
    Junior Member Krumpir's Avatar
    Join Date
    Jul 2012
    Location
    Cape Town, South Africa
    Posts
    9
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: how operator( && or || works).....

    Those are used to represent boolean operators. The && is used for AND statements and the || for OR statements.

    The expression "A || B" will return true if either A or B where "A && B" will only return true if both A and B are true.

    if(true || false)
    //code will run
    if(false || false)
    //code will not run
    if(true && false)
    //code will not run
    if(true && true)
    //code will run
    Check out my site -> tssolutions.net16.net

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how operator( && or || works).....

    Quote Originally Posted by Krumpir View Post
    Those are used to represent boolean operators. The && is used for AND statements and the || for OR statements.

    The expression "A || B" will return true if either A or B where "A && B" will only return true if both A and B are true.

    if(true || false)
    //code will run
    if(false || false)
    //code will not run
    if(true && false)
    //code will not run
    if(true && true)
    //code will run

    Sir, These Things i know .
    But my question is different

    e.g int k=5&4; give result k=4;

    but i fail to decide the result of int k=4&2&6;

    the same problem is with OR operator....

  5. #5
    Junior Member Krumpir's Avatar
    Join Date
    Jul 2012
    Location
    Cape Town, South Africa
    Posts
    9
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: how operator( && or || works).....

    Basic OR logic:
    x y x&y
    --------------
    1 0 | 0
    1 1 | 1
    0 1 | 0
    0 0 | 0

    In binary the numbers are represented as follow:
    4: 100
    2: 010
    6: 110

    The and operator will only return 1 when all 3 bits are 1.

    4: 1 0 0
    2: 0 1 0
    6: 1 1 0
    ----------
    0 0 0

    Thus the value of 4&2&6 is zero.

    Using the OR operator if only one bit needs to be 1 in order to return 1;

    4: 1 0 0
    2: 0 1 0
    6: 1 1 0
    ----------
    1 1 0

    Thus the value of 4&2&6 is 6.

    I hope you understand now, otherwise there is another explanation here: Bitwise operators in java
    Check out my site -> tssolutions.net16.net

  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how operator( && or || works).....

    Quote Originally Posted by Krumpir View Post
    Basic OR logic:
    x y x&y
    --------------
    1 0 | 0
    1 1 | 1
    0 1 | 0
    0 0 | 0

    In binary the numbers are represented as follow:
    4: 100
    2: 010
    6: 110

    The and operator will only return 1 when all 3 bits are 1.

    4: 1 0 0
    2: 0 1 0
    6: 1 1 0
    ----------
    0 0 0

    Thus the value of 4&2&6 is zero.

    Using the OR operator if only one bit needs to be 1 in order to return 1;

    4: 1 0 0
    2: 0 1 0
    6: 1 1 0
    ----------
    1 1 0

    Thus the value of 4&2&6 is 6.

    I hope you understand now, otherwise there is another explanation here: Bitwise operators in java



    Thanks ......

Similar Threads

  1. meaning of operator ( |= )
    By ashish12169 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 18th, 2012, 02:05 AM
  2. How to use final operator.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 9th, 2011, 06:57 PM
  3. Error with OR operator
    By tarkal in forum Loops & Control Statements
    Replies: 3
    Last Post: September 4th, 2011, 02:49 PM
  4. about Operator Precedence
    By degar in forum Java Theory & Questions
    Replies: 6
    Last Post: August 12th, 2011, 01:57 AM
  5. Operator Problem
    By KevinGreen in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 2nd, 2009, 09:50 AM