How can I read the bit number 30 of an 32-bit interger?
I've tried the following:
but is doesn't work.Code:int lp = msg->lParam; lp <<= 29; if(lp & 1) {.....} // bit number 30 is 1 else {...} //bit number 30 is 0
Thanks in advance!
How can I read the bit number 30 of an 32-bit interger?
I've tried the following:
but is doesn't work.Code:int lp = msg->lParam; lp <<= 29; if(lp & 1) {.....} // bit number 30 is 1 else {...} //bit number 30 is 0
Thanks in advance!
You don't need to shift any bits. Just mask the bit you're looking for:
Code:if ( lp & 0x02000000 )
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
Ok, thanks for help.
But are you sure this checks the 30th bit of a DWORD? To make it more clear what i'm trying to do see here(in the remarks section, second paragraph).
Last edited by Phenom; 07-05-2010 at 12:47 PM.
No, there's a zero too few.
00100000 00000000 00000000 00000000 in hexadecimal is: 0x20000000
Hence, that is the mask you are looking for.
If this is of any help, I have a small utility class that may help you here:
http://filebeam.com/0e941f14ec1412e56f7d67f488673f78
You basically just create a Stuff::Bit and pass the lParam into the constructor, then you can use the index operator to check if a particular bit is set.
You can also set and clear bits.
I'll try your class, but I have found a way to do what I want without all this bit stuff, although it's more complicated.
I guess it's up to me now. Thanks everyone for help.