Public Group

# a simple question regarding a variable definition

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

## Recommended Posts

I was looking up some stuff on bullet physics (here:Bullet Physics - Collision Filtering) and I found a peculiar variable definition on the last line:

#define BIT(x) (1<<(x))
enum collisiontypes {
COL_NOTHING = 0, //<Collide with nothing
COL_SHIP = BIT(0), //<Collide with ships
COL_WALL = BIT(1), //<Collide with walls
COL_POWERUP = BIT(2) //<Collide with powerups
}

int shipCollidesWith = COL_WALL;
int wallCollidesWith = COL_NOTHING;
int powerupCollidesWith = COL_SHIP | COL_WALL; <---- THIS LINE HERE!


What is this kind of definition is this and what is it usually used for?

##### Share on other sites

It's bitwise (logical) or. So it is

BIT(0) | BIT(1) = (1<<0) | (1<<1) = 1 | 2 = 3

i.e. bits 0 and 1 set. Since the bits are distinct, + would work just as well as |, but with | the intent is clear (a power up collides with either a ship or a wall or both).

##### Share on other sites

It's bitwise (logical) or. So it is

BIT(0) | BIT(1) = (1<<0) | (1<<1) = 1 | 2 = 3

i.e. bits 0 and 1 set. Since the bits are distinct, + would work just as well as |, but with | the intent is clear (a power up collides with either a ship or a wall or both).

I understand the idea, but I don't see what the part that I put in bold is for. I know it has something to do with bitwise operations, but I don't understand it in this context. Thank you for your reply.

Edited by Aluthreney

##### Share on other sites

It's logical shift left, which multiplies the left operand by 2N (where N is the right hand operand).

A left shift applied to the number 1 (just 1 bit set, the least significant bit, or "bit 0" as it is commonly called, so the bits in a 32-bit wide integer are bits 0 to 31, 31 being the most significant bit), sets the nth bit in the integer.

1 << 0 = 1 = 1 in binary

1 << 1 = 2 = 10 in binary

1 << 3 = 4 = 100 in binary

1 << 4 = 8 = 1000 in binary

1 << 5 = 16 = 10000 in binary

etc.

EDIT: So it is a way of packing up to 32 boolean values into a 32 bit integer (or 16 in a short, 64 in a 64 bit int, etc.).

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633703
• Total Posts
3013453
×