# & bitwise help

#### larswik

##### macrumors 68000
Original poster
I am learning Sprite Kit and dealing with collisions of objects. I got this snippet of code from the Apple Docs and I am trying to figure out what the &bitwise operator is doing. First time using bitwise. I know the && logic operator evaluates left and right side for a true or false.

Is it checking left and right to make sure both are not equal to 0?
Code:
``````static const uint32_t landerCatagory = 0x1;
static const uint32_t rockCatagory = 0x1 << 1;
static const uint32_t photonCatagory = 0x1 << 2;
static const uint32_t edgeCatagory = 0x1 << 3;

-(void)didBeginContact:(SKPhysicsContact *)contact{
SKPhysicsBody *firstBody;
SKPhysicsBody *secondBody;

firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
[B]
if ((firstBody.categoryBitMask & photonCatagory) != 0) [/B]{
}

NSLog(@"contact");
}``````

#### JohnsonK

##### macrumors regular
It is a bitwise AND operation.

0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1

#### larswik

##### macrumors 68000
Original poster
OK, I see what is happening there. Thanks for the reply.

-Lars

#### robvas

##### macrumors 68030
Also, a bitmask is a way of using every bit in a value to test for true or false.

int Thing1True = 1; // 0000 0000 0000 0001
int Thing1False = 0; // 0000 0000 0000 0000
int Thing2True = 1; // 0000 0000 0000 0001
int Thing2False = 0; // 0000 0000 0000 0000
int Thing3True = 1; // 0000 0000 0000 0001
int Thing3False = 0; // 0000 0000 0000 0000

(you're 'wasting' all those bits)

You can do stuff like bit-shifting and bit masks to set each individual bit in an int. So then you can fit 32 or 64 'true/false' in a single variable instead of 32 or 64 separate variables! It was more common in the old day when computers didn't have gigabytes of RAM.