Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How to check if a bit is off? (http://www.velocityreviews.com/forums/t285087-how-to-check-if-a-bit-is-off.html)

 Siemel Naran 08-18-2004 04:51 AM

How to check if a bit is off?

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;

 Gianni Mariani 08-18-2004 05:01 AM

Re: How to check if a bit is off?

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.

 Siemel Naran 08-18-2004 05:32 AM

Re: How to check if a bit is off?

"Gianni Mariani" <gi2nospam@mariani.ws> 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?

 Risto Lankinen 08-18-2004 05:53 AM

Re: How to check if a bit is off?

"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote in message
news:1fBUc.220126\$OB3.124204@bgtnsc05-news.ops.worldnet.att.net...
> 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 -

 Niels Dybdahl 08-18-2004 07:28 AM

Re: How to check if a bit is off?

> 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

 Old Wolf 08-18-2004 10:04 PM

Re: How to check if a bit is off?

"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote:
> "Gianni Mariani" <gi2nospam@mariani.ws> 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.

 Richard Herring 08-20-2004 02:36 PM

Re: How to check if a bit is off?

In message <843a4f78.0408181328.26435b8e@posting.google.com >, Old Wolf
<oldwolf@inspire.net.nz> writes
>"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote:
>> "Gianni Mariani" <gi2nospam@mariani.ws> 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

 Old Wolf 08-24-2004 04:27 AM

Re: How to check if a bit is off?

Richard Herring <junk@[127.0.0.1]> wrote:
> Old Wolf writes:
> >"Siemel Naran" <SiemelNaran@REMOVE.att.net> 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)

 Richard Herring 08-24-2004 08:38 AM

Re: How to check if a bit is off?

In message <843a4f78.0408232027.16307f74@posting.google.com >, Old Wolf
<oldwolf@inspire.net.nz> writes
>Richard Herring <junk@[127.0.0.1]> wrote:
>> Old Wolf writes:
>> >"Siemel Naran" <SiemelNaran@REMOVE.att.net> 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

 All times are GMT. The time now is 07:57 PM.