# Better way to do this?

• 01-19-2008
ryeguy
Better way to do this?
I know there's a term for this - not sure what it is. Anyway, I have a 2d tile engine for a game. In the tile data, there is a single number for the collision data. Each quadrant of the tile has it's own collision detection, so all the flags are represented as one number.

Maybe it would be easier to understand if I pasted my code:
Code:

```stringstream stream(buffer);                         int collision;                         //converts the hex value into an int                         stream>>hex>>collision;                         //if collision is 0 then it doesn't collide with anything                         if (collision>0)                         {                                 if (collision>=8)                                 {                                         collision-=8;                                         collideBR=true;                                 }                                 if (collision>=4)                                 {                                         collision-=4;                                         collideBL=true;                                 }                                 if (collision>=2)                                 {                                         collision-=2;                                         collideTR=true;                                 }                                 if (collision>=1)                                 {                                         collision-=1;                                         collideTL=true;                                 }                         }```
This works fine, does what I need. It just seems sloppy to me. The collideTL BL etc stands for top right, bottom left, etc. Is there a cleaner way to do this?
• 01-20-2008
Salem
Code:

```collideBR = ( collision & 0x8 ) != 0; collideBL = ( collision & 0x4 ) != 0;```
and so on for the other two.
• 01-20-2008
ryeguy
Thanks! That is what I was looking for. However, I noticed that I can achieve the same result without having the "!= 0".
• 01-20-2008
laserlight
Quote:

However, I noticed that I can achieve the same result without having the "!= 0".
That is because non-zero integers are converted to true when cast to bool, and zero is cast to false. The "!= 0" is probably good to keep since it clarifies that you are assigning a boolean, not integral, value.