Jump to content
  • Advertisement
Sign in to follow this  
duesjov

Bitwise operations - not my strong suit

This topic is 2106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.

 

Im looking in to the article on the frontpage http://www.gamedev.net/page/resources/_/technical/game-programming/implementing-component-entity-systems-r3382 about component entity systems..

 

bitwise operations have never been my strong suit,

 

I am looking at this line here

if((world->mask[entity] & MOVEMENT_MASK) == MOVEMENT_MASK)

in this part

#define MOVEMENT_MASK (COMPONENT_DISPLACEMENT | COMPONENT_VELOCITY)

void movementFunction(World *world)
{
    unsigned int entity;
    Displacement *d;
    Velocity *v;

    for(entity = 0; entity < ENTITY_COUNT; ++entity)
    {
        if((world->mask[entity] & MOVEMENT_MASK) == MOVEMENT_MASK)
        {
            d = &(world->displacement[entity]);
            v = &(world->velocity[entity]);

            v->y -= 0.98f;

            d->x += v->x;
            d->y += v->y;
        }
    }
}

and cant figure out why this is needed.

 
if((world->mask[entity] & MOVEMENT_MASK) == MOVEMENT_MASK)
 

 

couldn't you just put it like this ->

if(world->mask[entity] == MOVEMENT_MASK)

I hope you can help clarify this for me

 

Best Regards

Morten

 

Share this post


Link to post
Share on other sites
Advertisement

Using the & masks out the other bits, so that if other bits are set it won't affect the check. It's basically saying "if the MOVEMENT_MASK bits are set do this". Using a straight == would be saying "if the MOVEMENT_MASK bits are set and none of the other bits are set do this".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!