how to check a particular bit of a DWORD value?

G

Guest

I would need something like this:

DWORD array_of_bits;
int bit_number =5;
if ( bit_is_set(&array_of_bits, bit_number) ) then
{
do_something
}

any help highly appreciated
 
M

mlimber

I would need something like this:

DWORD array_of_bits;
int bit_number =5;
if ( bit_is_set(&array_of_bits, bit_number) ) then
{
do_something
}

any help highly appreciated

Either use std::bitset or the logical-and operator:

if( array_of_bits & (1<<bit_number) )
{
// ...
}

where bit_number is 0-based.

Cheers! --M
 
S

Stefan Naewe

I would need something like this:

DWORD array_of_bits;
int bit_number =5;
if ( bit_is_set(&array_of_bits, bit_number) ) then
{
do_something
}

any help highly appreciated

Untested (assuming a DWORD is 32 bits wide):

typedef std::bitset<32> bitarray;
bitarray ba(array_of_bits);

if(ba.test(5)) // or .test(4) depending where you start counting! LSB is 0
{
do_something();
}

/S
 
G

Guest

thanks, I hope this will work cause I need the DWORD to be at least 32 bits
wide on win2000 and xp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top