Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8541455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:50:09+00:00 2026-06-11T11:50:09+00:00

I’m messing around with bitwise operations and wanted to implement the simple logic puzzle

  • 0

I’m messing around with bitwise operations and wanted to implement the simple logic puzzle where you have a fox(wolf), chicken(goose), grain(corn) and a person trying to cross a river. I’m using the first 4 bits for each one to represent which side of the river they are on.

I’m having a little bit of trouble trying to implement the logic.

How can I check if the two bits are either 1 or 0 but not 1 and 0?

int game()
{
    int state = 0;
    int done = 0;
    while (!done)
    {
        int input = 0;
        input = move();

        /*
           0000 0000
                |||\_grain
                ||\__chicken
                |\___fox
                \____person
           0 = left side of the river
           1 = right side
        */
        if (input == 3)// Moves person and grain
        {
            // Move the grain if the person is on the same side.
            if (!(state & 1 << 3 ^ state & 1<< 0))
            {
                state ^= 1 << 3;
                state ^= 1 << 0;
            }
            else
            {
                // Always switch the person no matter what
                state ^= 1 << 3;
            }
        }
        else if (input == 2) // Moves person and chicken
        {
            // Move Chicken only if person is on the same side
            if (!(state & 1 << 3 ^ state & 1<< 1))
            {
                state ^= 1 << 3;
                state ^= 1 << 1;
            }
            else
            {
                // Always switch the person no matter what
                state ^= 1 << 3;
            }
        }
        else if (input == 1)// Moves person and fox
        {
            // Move the fox if the person is on the same side.
            if (!(state & 1 << 3 ^ state & 1<< 2))
            {
                state ^= 1 << 3;
                state ^= 1 << 2;
            }
            else
            {
                // Always switch the person no matter what
                state ^= 1 << 3;
            }
        }

        // Fox and Chicken on one side and person on the other = lost
        if ((state & 1 << 2 && state & 1 << 1) && ~(state & 1 << 3))
        {
            printf("Failed\n");
        }
    }
    return 1;
}

I’m guessing bitwise checking would be more elegant code but it seems like it’s more of a pain. I always end up doing this when I get sick of hitting my head against the wall with bitwise logic.

int game()
{
    int state = 0;
    int done = 0;
    while (!done)
    {
        int input = 0;
        input = move();

        /*
           0000 0000
                   | | | \_grain
                   | | \__chicken
                   | \___fox
                   \____person
           0 = left side of the river
           1 = right side
        */
        if (input == 3)// Moves person and grain
        {
            // Are they on the same side?
            if (state == 9 || state == 11 || state == 13 || state == 15 ||
                state == 0 || state == 2 || state == 4 || state == 6)
            {
                // Move the person and grain
                state ^= 1 << 3;
                state ^= 1 << 0;
            }
            else
            {
                state ^= 1 << 3;
            }
        }
        else if (input == 2) // Moves person and chicken
        {
            // Are they on the same side?
            if (state == 10 || state == 11 || state == 14 || state == 15 ||
                state == 0 || state == 1 || state == 4 || state == 5)
            {
                // Move the person and chicken
                state ^= 1 << 3;
                state ^= 1 << 1;
            }
            else
            {
                state ^= 1 << 3;
            }
        }
        else if (input == 1)// Moves person and fox
        {
            // Are they on the same side?
            if (state == 12 || state == 13 || state == 14 || state == 15 ||
                state == 0 || state == 1 || state == 2 || state == 3)
            {
                // Move the person and fox
                state ^= 1 << 3;
                state ^= 1 << 2;
            }
            else
            {
                state ^= 1 << 3;
            }
        }
        else
        {
            // Always switch the person no matter what
            state ^= 1 << 3;
        }

        //Check if you won or lost
        if (state == 3 || state == 6 || state == 8 || state == 9 || state == 12) // Lost
        {
            return 0;
        }
        if (state == 15) // Won
        {
            return 1;
        }
    }
    return 1;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:50:10+00:00Added an answer on June 11, 2026 at 11:50 am

    How can I check if the two bits are either 1 or 0 but not 1 and 0?

    How about comparing them for equality?

    #define MAN_MASK   (1<<3)
    #define GRAIN_MASK (1<<0)
    
    if(  
         ((state & MAN_MASK) == 0)
                == 
         ((state & GRAIN_MASK) == 0)
      );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.