Bitwise question

A

Alessio

Hi,

I'm wondering if I want to check if a bit is not set,
the following statements are valid:

/* check if bit is not set */
if ( !(MASK & BIT_TO_CHECK) )
{
/* do something */
}

and

/* check if bit is not set */
if ( MASK & ~BIT_TO_CHECK )
{
/* do something */
}

Thank you,
Alessio
 
A

asit

Hi,

I'm wondering if I want to check if a bit is not set,
the following statements are valid:

/* check if bit is not set */
if ( !(MASK & BIT_TO_CHECK) )
{
        /* do something */

}

and

/* check if bit is not set */
if ( MASK & ~BIT_TO_CHECK )
{
        /* do something */

}

Thank you,
Alessio


bit = number & (1 << BIT_TO_CHECK);
if(bit)
{
//bit is set
}
else
{
//bit is not set
}
 
A

Alessio

asit ha scritto:
bit = number & (1 << BIT_TO_CHECK);
if(bit)
{
//bit is set
}
else
{
//bit is not set
}

Thank you for your quick reply, but my question was different!
 
E

Eric Sosman

Alessio said:
Hi,

I'm wondering if I want to check if a bit is not set,
the following statements are valid:

/* check if bit is not set */
if ( !(MASK & BIT_TO_CHECK) )
{
/* do something */
}

This does "something" if MASK and BIT_TO_CHECK have
no 1-bits at corresponding positions, that is, if any
1-bit in MASK corresponds to a 0-bit in BIT_TO_CHECK.
and

/* check if bit is not set */
if ( MASK & ~BIT_TO_CHECK )
{
/* do something */
}

This does "something" if MASK has any 1-bit at the same
position where BIT_TO_CHECK has a 0-bit. Same effect ...

... except that there's a small risk of trouble with the ~
operator if BIT_TO_CHECK is a signed type (best avoided when
doing bit-fiddling) or if it promotes to a signed type (likely
if BIT_TO_CHECK is an unsigned char, unsigned short, or unsigned
bit-field). The first form has no such risk.
 
S

Seebs

I'm wondering if I want to check if a bit is not set,
the following statements are valid:
/* check if bit is not set */
if ( !(MASK & BIT_TO_CHECK) )
{
/* do something */
}

This is.
/* check if bit is not set */
if ( MASK & ~BIT_TO_CHECK )
{
/* do something */
}

No. This checks whether any other bits are set, which is not the same
question.

-s
 
S

Seebs

This does "something" if MASK and BIT_TO_CHECK have
no 1-bits at corresponding positions,
Agreed.

that is, if any
1-bit in MASK corresponds to a 0-bit in BIT_TO_CHECK.

I don't think so.

Imagine that MASK = 0x111 and BIT_TO_CHECK = 0x011.

A 1-bit in MASK (0x100) corresponds to a 0 bit in BIT_TO_CHECK (0x0..),
but MASK & BIT_TO_CHECK is still 0x011, so !(MASK & BIT_TO_CHECK) is
0.
This does "something" if MASK has any 1-bit at the same
position where BIT_TO_CHECK has a 0-bit. Same effect ...

Right description, but I think not the same.

-s
 
W

Willem

Seebs wrote:
)> I'm wondering if I want to check if a bit is not set,
)> the following statements are valid:
)
)> /* check if bit is not set */
)> if ( !(MASK & BIT_TO_CHECK) )
)> {
)> /* do something */
)> }
)
) This is.
)
)> /* check if bit is not set */
)> if ( MASK & ~BIT_TO_CHECK )
)> {
)> /* do something */
)> }
)
) No. This checks whether any other bits are set, which is not the same
) question.

You can fix it though, by moving the ~ to a different location.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top