Adapt > Engage > Dominate

Bit Masking

Bit masking is the stuff seen in calls to certain DirectX functions and many other functions in many libraries.  Function( Something | something else | something other ).  The enum values must be powers of 2 or masking will fail.  Take 10 and 101, OR them together -> 111.  Now try and use bit masking with 100.  100 AND 111 -> 100.  So we are getting a false positive because 100 or 4 is not a power of 2.  One way to easily enter this into enums is to do 0, 1 << 0, 1 << 1, 1 << 2, etc.  This shifts the 1 bit over by the number on the right, giving a power of 2 value and a relatively clear and readable way to generate values usable for masking.  Library here for typesafe bitwise ops based on enums.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: