Check if the 20th bit of a 32 bit integer is on or off?

AND it with x00001000 and check if its equal to x00001000

if((num & x00001000)==x00001000)

Note that the digits represented here are in hex.

0      0      0      0      1      0      0      0
^
|

x0000   0000   0000   0000   0001   0000   0000   0000 = 32 bits

^                              ^                    ^
|                              |                    |

0th bit                        20th bit             32nd bit

Advertisements

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