Bitwise operations - not my strong suit

Started by
1 comment, last by duesjov 10 years, 7 months ago

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

www.mojostudios.dk

New Relealse - Rainbow Run (IOS, Android)

Get it on Play store

Get it on App store

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".

ahh I see.

Thank you for the quick response.

Regards

www.mojostudios.dk

New Relealse - Rainbow Run (IOS, Android)

Get it on Play store

Get it on App store

This topic is closed to new replies.

Advertisement