Results 1 to 9 of 9

Thread: wow this is really weird..

  1. #1
    Join Date
    Mar 2007
    Posts
    107
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default wow this is really weird..

    is there any reason why this should not print 3333?
    int i=0,j[]={1,2};
    std::cout<<(j[i]|j[++i]);
    std::cout<<(j[i--]|j[i]);
    std::cout<<(j[i++]|j[i]);
    std::cout<<(j[i]|j[--i]);
    std::cout<<std::endl;
    im using vc++ 2008 express and for me it prints 2211.

  2. #2
    Join Date
    Dec 2006
    Location
    Banville
    Posts
    3,914
    Mentioned
    12 Post(s)
    Quoted
    98 Post(s)

    Default

    You know what the OR ioerator does, right? Build a logic table.

    Code:
    int i=0,j[]={1,2};
    
    std::cout<<j[i]|j[++i];
    std::cout<<j[i--]|j[i];
    std::cout<<j[i++]|j[i];
    std::cout<<j[i]|j[--i];
    std::cout<<std::endl;
    The jealous temper of mankind, ever more disposed to censure than
    to praise the work of others, has constantly made the pursuit of new
    methods and systems no less perilous than the search after unknown
    lands and seas.

  3. #3
    Join Date
    Mar 2007
    Posts
    107
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    2 or 1 = 3 (in binary thats 10 or 1 = 11) right?

  4. #4
    Join Date
    Dec 2007
    Location
    Somewhere in Idaho
    Posts
    480
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Well, your first problem is an order of operations problem, (at least it was with GCC) you need to preform the or before the bitshift operator (using parenthesis). However, even after doing that I was getting the incorrect answer (3213).

    The second problem I seem to be running into is that the ++ operator isn't being performed correctly. What it looks like is that the ++ operator is applied after the | operator, Parenthesis don't seem to help here. This may be compiler specific, so it may work for you.

    Here is what I used:
    Code:
    std::cout << (j[i] | j[++i]);
    std::cout << ((j[(i--)]) | j[i]);
    std::cout << ((j[(i++)]) | j[i]);
    std::cout << (j[i] | j[--i]);
    std::cout << std::endl;

  5. #5
    Join Date
    Dec 2006
    Location
    Banville
    Posts
    3,914
    Mentioned
    12 Post(s)
    Quoted
    98 Post(s)

    Default

    Seriosly. If you need to know the difference between i++ and ++i, its too complicated.
    The jealous temper of mankind, ever more disposed to censure than
    to praise the work of others, has constantly made the pursuit of new
    methods and systems no less perilous than the search after unknown
    lands and seas.

  6. #6
    Join Date
    Dec 2007
    Location
    Somewhere in Idaho
    Posts
    480
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    depends, A good programmer should know the difference between ++i and i++.

  7. #7
    Join Date
    Dec 2006
    Location
    Banville
    Posts
    3,914
    Mentioned
    12 Post(s)
    Quoted
    98 Post(s)

    Default

    But a good programmer would also know the value of simplicity.
    The jealous temper of mankind, ever more disposed to censure than
    to praise the work of others, has constantly made the pursuit of new
    methods and systems no less perilous than the search after unknown
    lands and seas.

  8. #8
    Join Date
    Aug 2006
    Location
    London
    Posts
    2,021
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    i++ and ++i, like everything, should be used in moderation
    when the situation is right, they are good

    i++ can aid simplicity

    example for you

    Code:
    int* arrayOfStuff;
    int* i = arrayOfStuff;
    
    int thing1 = *(i++);
    int thing2 = *(i++);
    int thing3 = *(i++);
    int thing4 = *(i++);
    
    //must simpler then this
    
    int thing5 = *i;
    i++;
    int thing6 = *i;
    i++;
    int thing7 = *i;
    i++;
    int thing8 = *i;
    i++;
    Join the Official SRL IRC channel. Learn how to Here.

  9. #9
    Join Date
    Mar 2007
    Posts
    107
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    lol i known the difference between postfix and prefix (i think thats what its called in java tuts) but if you look at each of those lines they should print 3.
    first line:
    j[i] (i=0 so j[0]=1) | j[++i] (++i=1 so j[1]=2)
    so 1 | 2 = 3 but im getting 2

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Weird
    By Colluci in forum OSR Help
    Replies: 7
    Last Post: 01-27-2009, 12:22 AM
  2. weird key
    By ZaSz in forum Research & Development Lounge
    Replies: 6
    Last Post: 02-19-2008, 11:39 AM
  3. Weird Eye + One Sig.
    By Wilio in forum Graphics and Multimedia
    Replies: 8
    Last Post: 09-04-2007, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •