# How to check if a bit is off?

Discussion in 'C++' started by Siemel Naran, Aug 18, 2004.

1. ### Siemel NaranGuest

Hi. How to check if a bit is off?

To check if a bit is on we do

return d_flags & flag;

where flag is one flag. To check if a bit is off would this work?

return ~d_flags & flag;

Siemel Naran, Aug 18, 2004

2. ### Gianni MarianiGuest

Siemel Naran wrote:
> Hi. How to check if a bit is off?
>
> To check if a bit is on we do
>
> return d_flags & flag;
>
> where flag is one flag. To check if a bit is off would this work?
>
> return ~d_flags & flag;
>
>
>

return !( d_flags & flag );

What you have will work as well.

Gianni Mariani, Aug 18, 2004

3. ### Siemel NaranGuest

"Gianni Mariani" <> wrote in message

> > To check if a bit is on we do
> >
> > return d_flags & flag;
> >
> > where flag is one flag. To check if a bit is off would this work?
> >
> > return ~d_flags & flag;
> >
> >
> >

>
> return !( d_flags & flag );
>
> What you have will work as well.

Question: what is the type of !(d_flags & flag ).
The type of (d_flags & flag) is int, assuming d_flags and flag are enums
that are converted to int.
The type of !(d_flags & flag) is bool, right?
This would mean converting an int to bool, which I imagine is internally
something like

if (value != 0) result = 1;
else result = 0;

Thus !(d_flags & flag) is

if (d_flags & flag == 0) result = 1;
else result = 0;

But would

return ~d_flags & flag;

be faster?

Siemel Naran, Aug 18, 2004
4. ### Risto LankinenGuest

"Siemel Naran" <> wrote in message
news:1fBUc.220126\$...
> Hi. How to check if a bit is off?
>
> To check if a bit is on we do
>
> return d_flags & flag;
>
> where flag is one flag. To check if a bit is off would this work?
>
> return ~d_flags & flag;

If the definition of "off" is "not on", then this will work:

return !(d_flags & flag);

Cheers!

- Risto -

Risto Lankinen, Aug 18, 2004
5. ### Niels DybdahlGuest

> The type of !(d_flags & flag) is bool, right?

Yes.

> But would
>
> return ~d_flags & flag;
>
> be faster?

No. In the best case the compiler will optimize both expressions to the same
code. In the worst case !(d_flags & flag) will be compiled into one "and"
operation and one conditional jump, while (~d_flags & flag) will become one
negation, one "and" and one conditional operation.

Niels Dybdahl

Niels Dybdahl, Aug 18, 2004
6. ### Old WolfGuest

"Siemel Naran" <> wrote:
> "Gianni Mariani" <> wrote:
>
> > > To check if a bit is on we do
> > > return d_flags & flag;
> > > where flag is one flag. To check if a bit is off would this work?
> > > return ~d_flags & flag;

> >
> > return !( d_flags & flag );

>
> Question: what is the type of !(d_flags & flag ).

The results of && || ! are all int.

> The type of !(d_flags & flag) is bool, right?

No

> This would mean converting an int to bool, which I imagine is internally
> something like
>
> if (value != 0) result = 1;
> else result = 0;
>
> Thus !(d_flags & flag) is
>
> if (d_flags & flag == 0) result = 1;
> else result = 0;

I don't know why so many people have misgivings about "int to bool
conversions". false is zero and true is non-zero. This was the case
even before computers were invented. No assembly instructions are
required.

> But would
>
> return ~d_flags & flag;
>
> be faster?

Why don't you do some profiling. Both cases involve 2 operations.

Old Wolf, Aug 18, 2004
7. ### Richard HerringGuest

In message <>, Old Wolf
<> writes
>"Siemel Naran" <> wrote:
>> "Gianni Mariani" <> wrote:
>>
>> > > To check if a bit is on we do
>> > > return d_flags & flag;
>> > > where flag is one flag. To check if a bit is off would this work?
>> > > return ~d_flags & flag;
>> >
>> > return !( d_flags & flag );

>>
>> Question: what is the type of !(d_flags & flag ).

>
>The results of && || ! are all int.

Not according to ISO14882 sections 5.3.1, 5.14 and 5.15:
"The result is a bool".

>
>> The type of !(d_flags & flag) is bool, right?

>
>No

Yes [ibid.]
>
>> This would mean converting an int to bool, which I imagine is internally
>> something like
>>
>> if (value != 0) result = 1;
>> else result = 0;
>>
>> Thus !(d_flags & flag) is
>>
>> if (d_flags & flag == 0) result = 1;
>> else result = 0;

>
>I don't know why so many people have misgivings about "int to bool
>conversions".

I don't know why so many people have misgivings about the argument and
result types of !, && and ||.

>false is zero and true is non-zero. This was the case
>even before computers were invented. No assembly instructions are
>required.

There's at least one architecture where even=>false and odd=>true.
>
>> But would
>>
>> return ~d_flags & flag;
>>
>> be faster?

>
>Why don't you do some profiling. Both cases involve 2 operations.

--
Richard Herring

Richard Herring, Aug 20, 2004
8. ### Old WolfGuest

Richard Herring <junk@[127.0.0.1]> wrote:
> Old Wolf writes:
> >"Siemel Naran" <> wrote:
> >>
> >> Question: what is the type of !(d_flags & flag ).

> >
> >The results of && || ! are all int.

>
> Not according to ISO14882 sections 5.3.1, 5.14 and 5.15:
> "The result is a bool".

How right you are

> >> This would mean converting an int to bool, which I imagine is internally
> >> something like
> >>
> >> if (value != 0) result = 1;
> >> else result = 0;
> >>
> >> Thus !(d_flags & flag) is
> >>
> >> if (d_flags & flag == 0) result = 1;
> >> else result = 0;

> >
> >I don't know why so many people have misgivings about "int to bool
> >conversions".

>
> I don't know why so many people have misgivings about the argument and
> result types of !, && and ||.

It's different in C. But this is a different issue to int-to-bool

> >false is zero and true is non-zero. This was the case
> >even before computers were invented. No assembly instructions are
> >required.

>
> There's at least one architecture where even=>false and odd=>true.

Irrelevant to C++ (as was the original point, too)

Old Wolf, Aug 24, 2004
9. ### Richard HerringGuest

In message <>, Old Wolf
<> writes
>Richard Herring <junk@[127.0.0.1]> wrote:
>> Old Wolf writes:
>> >"Siemel Naran" <> wrote:
>> >>
>> >> Question: what is the type of !(d_flags & flag ).
>> >
>> >The results of && || ! are all int.

>>
>> Not according to ISO14882 sections 5.3.1, 5.14 and 5.15:
>> "The result is a bool".

>
>How right you are
>
>> >> This would mean converting an int to bool, which I imagine is internally
>> >> something like
>> >>
>> >> if (value != 0) result = 1;
>> >> else result = 0;
>> >>
>> >> Thus !(d_flags & flag) is
>> >>
>> >> if (d_flags & flag == 0) result = 1;
>> >> else result = 0;
>> >
>> >I don't know why so many people have misgivings about "int to bool
>> >conversions".

>>
>> I don't know why so many people have misgivings about the argument and
>> result types of !, && and ||.

>
>It's different in C.

It would have to be. C had no bool type.

>But this is a different issue to int-to-bool
>
>> >false is zero and true is non-zero. This was the case
>> >even before computers were invented. No assembly instructions are
>> >required.

>>
>> There's at least one architecture where even=>false and odd=>true.

>
>Irrelevant to C++ (as was the original point, too)

You're the one who raised assembly instructions.

--
Richard Herring

Richard Herring, Aug 24, 2004