Sign in to follow this  

Question about BITS!

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible in c++ to read the value of a particular bit? i think you could like.. use the bit operators to copy a bit into a new word and read that value.. i think.. is there another way? its no big deal im just curious and by read value of a bit i mean read whether a particular bit is on or off, of course

Share this post


Link to post
Share on other sites
Yes you can get the value of individual bit.

here is example

short GetBit(short word, short bitpos){
short mask = 0;
short value = 0;

mask = pow(2, bitpos);

value = (word & mask) / mask;

return value;

}




You can also use bit fields.

Share this post


Link to post
Share on other sites
cool thanks. be very easy to ignore ancillary chunks in png spec for now then :p

edit: actually what am I saying, i dont need that to ignore chunks at all.. still, useful anyway :P

Share this post


Link to post
Share on other sites
Hi there, I love bit operations, I also love assembly. Anyway, I didn't like the the division in prh99;s algorithm, so here is something that I wrote that I believe works with built in datatype.


/*
Written by Ordered_Disorder for the gamedev forums
jan 29, 05 12:12 am
*/

template <class T>
bool GetBitValue(T variable, const unsigned int bit_pos)
{
T bit_power = 1<<bit_pos;

if (((int)variable & (int)bit_power) == bit_power )
return true;
else
return false;
}





PS: kudos libvan for reminding me about <<

Share this post


Link to post
Share on other sites

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this