I'm currently reading the "SFML game development" book and in Chapter 4 - Input Handling in the sub-chapter "Receiver Category", paragraph n°2; we've got this:
"We define an enum to refer to the different categories. Each category except None is initialized with an integer that has one bit set to 1 , and the rest are set to 0 :"
namespace Category
{
enum Type
{
None = 0,
Scene = 1 << 0,
PlayerAircraft = 1 << 1,
AlliedAircraft = 1 << 2
EnemyAircraft = 1 << 3,
};
}
I'm not really comfortable with bitwise operators and binary operations in general; so I don't understand that part "Each category except None is initialized with an integer that has one bit set to 1 , and the rest are set to 0 :".
If each category (except None) is initialized as said above, what is "the rest" set to 0 ?!
Note: After double-reading I think I understand that each member of the enum is a byte and so, the first bit of those is set to 1 and the other bits of the same byte are set to 0.
So, Scene = 0b1, PlayerAircraft = 0b10 (one bit = 1, the other = 0), etc... so if I would have write : PlayerAircraft = 2 << 1 PlayerAircraft would have been equal to 0b11? am I right or I'm missing something?
Further in the chapter; we've got a if condition checking if the requested category is the same as the scene node one; without going off-subject; I didn't understand that part.
Since it uses a AND (or & if you prefer) bitwise operator; how could it check if the scene node category is the same as the requested one?
I've check on Wikipedia how it works but I didn't fully get it.
Here's the code
void SceneNode::onCommand(const Command& command, sf::Time dt)
{
if(command.category & getCategory()) //The part I don't understand
command.action(*this, dt);
/* ... */
}
I don't get it...
Voilà, thank you in advance and I hope my first post in here isn't too messy and that I have provided enough informations. If not, I will edit! :)
PS: Sorry for the lame English, I'm not awake enough today.
1<<0evaluates to 1 (00000001).1<<1evaluates to 2 (00000010).1<<2evaluates to 4 (00000100).1<<3evaluates to 8 (00001000). It is bit shifting, you can consider it as multiplying/dividing by two by left/right shifting respectively. I'm assuming this is being done so they can use abitmaskwith this enum later.