Thread: logic operators

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    15

    Question logic operators

    int main()
    {
    int x, y, z, q;
    x = y = z = 1;
    q = ++x || ++y && ++z; PRINT3 (x, y, z, q);
    /* x=2 y=1 z=1 q=1*/

    can someone explain to me whats going on here, i dont understand the logic and why some variables are being increamented and others are not

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Good lord... If I ever write code like that please shoot me!

    First off the logic operators are all wrong.

    | is binary OR as in 1 | 0 = 1
    || is logical or as in this or that.

    The same with the & ...
    & is binary AND as in 1 & 1 = 1
    && is logical and as in this and that.

    PRINT3 is not a C function so it must be defined someplace.

    ++ x | ++y & ++z would amount to 2 OR 2 AND 2 ... which has no choice but to equal 2

    The code, as posted, makes no sense to me at all.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    i got this in a test

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ashfire View Post
    i got this in a test
    Ouch.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    now you know how i feel about this i totally flunked the test apparently the commented values are the results of the logical operations

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    #define PRINT3(x,y,z,q) printf("x=%d\ty=%d\tz=%d\tq=%d\n",x,y,z,q)

  7. #7
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by ashfire View Post
    i got this in a test
    I almost fainted when I saw your code. I waited for someone else to comment
    first, fearing that maybe I'm drunk or off my rocker.

    My only input is this:

    main should be declared an int, and return an int upon successful
    completion:

    Code:
    int main(void)
    {  
           code here
    
    return 0;
    
    }

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    ok let me put the whole test
    insert
    Code:
    #include <stdio.h>
    #define PRINT3(x,y,z,q) printf("x=%d\ty=%d\tz=%d\tq=%d\n",x,y,z,q)
    int main()
    {
    int x, y, z, q;
    x = y = z = 1;
    q = ++x || ++y && ++z; PRINT3 (x, y, z, q);
    /* x=2 y=1 z=1 q=1 since the expression is equivalent to
    ++x || (++y && ++z); */
    x = y = z = 1;
    q = ++x && ++y || ++z; PRINT3 (x, y, z, q);
    /* x=2 y=2 z=1 q=1 since the expression is equivalent to
    (++x && ++y) || ++z; */
    x = y = z = 1;
    q = ++x && ++y && ++z; PRINT3 (x, y, z, q);
    /* x=2 y=2 z=2 q=1 */
    x = y = z = 1;
    q = ++x || ++y || ++z; PRINT3 (x, y, z, q);
    /* x=2 y=1 y=1 q=1 */
    x = y = z = 0;
    q = ++x && ++y || ++z; PRINT3 (x, y, z, q);
    /* x=1 y=1 z=0 q=1 */
    x = y = z = 0;
    q = ++x || ++y && ++z; PRINT3 (x, y, z, q);
    /* x=1 y=0 z=0 q=1 */
    x = y = z = 0;
    q = ++x && ++y && ++z; PRINT3 (x, y, z, q);
    /* x=1 y=1 z=1 q=1 */
    x = y = z = 0;
    q = ++x || ++y || ++z; PRINT3 (x, y, z, q);
    /* x=1 y=0 z=0 q=1 */
    x = y = z = -1;
    q = ++x && ++y || ++z; PRINT3 (x, y, z, q);
    /* x=0 y=-1 z=0 q=0 */
    x = y = z = -1;
    q = ++x || ++y && ++z; PRINT3 (x, y, z, q);
    /* x=0 y=0 z=-1 q=0 */
    x = y = z = -1;
    q = ++x && ++y && ++z; PRINT3 (x, y, z, q);
    /* x=0 y=-1 z=-1 q=0 */
    x = y = z = -1;
    q = ++x || ++y || ++z; PRINT3 (x, y, z, q);
    /* x=0 y=0 z=0 q=0 */
    return 0;
    }
    hell on earth

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    This could be a part of the problem Sequence point - Wikipedia, the free encyclopedia

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Char*Pntr View Post
    I almost fainted when I saw your code. I waited for someone else to comment
    first, fearing that maybe I'm drunk or off my rocker.
    I know the feeling... and I don't drink alcohol.

    EDIT: The really scary part is that it compiles and runs, giving the answers shown...
    Last edited by CommonTater; 10-30-2010 at 12:24 PM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ashfire
    can someone explain to me whats going on here, i dont understand the logic and why some variables are being increamented and others are not
    Among other things, this question tests whether you understand the short-circuit behaviour of || and &&. Now, ++x evaluates to 2, which evaluates to true, hence the left hand side of the || evaluates to true. Since this is sufficient to determine the result of the entire || expression, evaluation stops here, hence y and z are not incremented. Furthermore, the result of the expression is 1 (i.e., the "canonical" true value), hence 1 is assigned to q.

    Quote Originally Posted by CommonTater
    First off the logic operators are all wrong.
    No, if the bitwise operators were used, there would not be this short-circuit/lazy evaluation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by CommonTater View Post
    I know the feeling... and I don't drink alcohol.

    EDIT: The really scary part is that it compiles and runs, giving the answers shown...
    Happy Halloween!

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    laserlight... If you understand this, how about picking a couple of lines and giving me a walk through... I have to admit this one just baffles me...

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Char*Pntr View Post
    Happy Halloween!
    LOL....

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    laserlight... If you understand this, how about picking a couple of lines and giving me a walk through... I have to admit this one just baffles me...
    I already went through ashfire's original question
    If you want another one, examine:
    Code:
    x = y = z = -1;
    q = ++x || ++y && ++z; PRINT3 (x, y, z, q);
    Now, ++x evaluates to 0, but this is not enough to determine the result of the || expression, so ++y && ++z is evaluated. ++y evaluates to 0, and this is enough to determine that the result of the && expression is 0, so evaluation stops here, meaning that ++z is not evaluated (thus z remains as -1). The result of the expression is 0, so 0 is assigned to q. This is how we get:
    Code:
    /* x=0 y=0 z=-1 q=0 */
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Handling logic and draw cycles
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 07-25-2009, 10:15 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  4. Finding out if a bit is set using logic operators...
    By minime6696 in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 04:24 AM

Tags for this Thread