Find given bit no ON/OFF for given Value

Posted: March 9, 2012 in OOP344

METHOD 1

bool isOn(unsigned int val, int bitNo){
  return (val>>bitNo)%2;
}

bool isOff(unsigned int val, int bitNo){
  return (~val>>bitNo)%2;
}

METHOD 2

bool isOn(unsigned int val, int bitNo){
  return ((val>>bitNo)!=(val>>(bitNo+1))<<1);
}

bool isOff(unsigned int val, int bitNo){
  return ((val>>bitNo)==(val>>(bitNo+1))<<1);
}

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s