Connecting Tech Pros Worldwide Help | Site Map

How to check if a bit is off?

  #1  
Old July 22nd, 2005, 07:00 PM
Siemel Naran
Guest
 
Posts: n/a
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;



  #2  
Old July 22nd, 2005, 07:00 PM
Gianni Mariani
Guest
 
Posts: n/a

re: How to check if a bit is off?


Siemel Naran wrote:[color=blue]
> 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;
>
>
>[/color]

return !( d_flags & flag );

What you have will work as well.
  #3  
Old July 22nd, 2005, 07:00 PM
Siemel Naran
Guest
 
Posts: n/a

re: How to check if a bit is off?


"Gianni Mariani" <gi2nospam@mariani.ws> wrote in message
[color=blue][color=green]
> > 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;
> >
> >
> >[/color]
>
> return !( d_flags & flag );
>
> What you have will work as well.[/color]

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?


  #4  
Old July 22nd, 2005, 07:00 PM
Risto Lankinen
Guest
 
Posts: n/a

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...[color=blue]
> 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;[/color]

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

return !(d_flags & flag);

Cheers!

- Risto -


  #5  
Old July 22nd, 2005, 07:00 PM
Niels Dybdahl
Guest
 
Posts: n/a

re: How to check if a bit is off?


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

Yes.
[color=blue]
> But would
>
> return ~d_flags & flag;
>
> be faster?[/color]

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


  #6  
Old July 22nd, 2005, 07:02 PM
Old Wolf
Guest
 
Posts: n/a

re: How to check if a bit is off?


"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote:[color=blue]
> "Gianni Mariani" <gi2nospam@mariani.ws> wrote:
>[color=green][color=darkred]
> > > 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;[/color]
> >
> > return !( d_flags & flag );[/color]
>
> Question: what is the type of !(d_flags & flag ).[/color]

The results of && || ! are all int.
[color=blue]
> The type of !(d_flags & flag) is bool, right?[/color]

No
[color=blue]
> 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;[/color]

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.
[color=blue]
> But would
>
> return ~d_flags & flag;
>
> be faster?[/color]

Why don't you do some profiling. Both cases involve 2 operations.
  #7  
Old July 22nd, 2005, 07:04 PM
Richard Herring
Guest
 
Posts: n/a

re: How to check if a bit is off?


In message <843a4f78.0408181328.26435b8e@posting.google.com >, Old Wolf
<oldwolf@inspire.net.nz> writes[color=blue]
>"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote:[color=green]
>> "Gianni Mariani" <gi2nospam@mariani.ws> wrote:
>>[color=darkred]
>> > > 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 );[/color]
>>
>> Question: what is the type of !(d_flags & flag ).[/color]
>
>The results of && || ! are all int.[/color]

Not according to ISO14882 sections 5.3.1, 5.14 and 5.15:
"The result is a bool".
[color=blue]
>[color=green]
>> The type of !(d_flags & flag) is bool, right?[/color]
>
>No[/color]

Yes [ibid.][color=blue]
>[color=green]
>> 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;[/color]
>
>I don't know why so many people have misgivings about "int to bool
>conversions".[/color]

I don't know why so many people have misgivings about the argument and
result types of !, && and ||.
[color=blue]
>false is zero and true is non-zero. This was the case
>even before computers were invented. No assembly instructions are
>required.[/color]

There's at least one architecture where even=>false and odd=>true.[color=blue]
>[color=green]
>> But would
>>
>> return ~d_flags & flag;
>>
>> be faster?[/color]
>
>Why don't you do some profiling. Both cases involve 2 operations.[/color]

--
Richard Herring
  #8  
Old July 22nd, 2005, 07:08 PM
Old Wolf
Guest
 
Posts: n/a

re: How to check if a bit is off?


Richard Herring <junk@[127.0.0.1]> wrote:[color=blue]
> Old Wolf writes:[color=green]
> >"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote:[color=darkred]
> >>
> >> Question: what is the type of !(d_flags & flag ).[/color]
> >
> >The results of && || ! are all int.[/color]
>
> Not according to ISO14882 sections 5.3.1, 5.14 and 5.15:
> "The result is a bool".[/color]

How right you are
[color=blue][color=green][color=darkred]
> >> 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;[/color]
> >
> >I don't know why so many people have misgivings about "int to bool
> >conversions".[/color]
>
> I don't know why so many people have misgivings about the argument and
> result types of !, && and ||.[/color]

It's different in C. But this is a different issue to int-to-bool
conversions, which the OP was asking about.
[color=blue][color=green]
> >false is zero and true is non-zero. This was the case
> >even before computers were invented. No assembly instructions are
> >required.[/color]
>
> There's at least one architecture where even=>false and odd=>true.[/color]

Irrelevant to C++ (as was the original point, too)
  #9  
Old July 22nd, 2005, 07:08 PM
Richard Herring
Guest
 
Posts: n/a

re: How to check if a bit is off?


In message <843a4f78.0408232027.16307f74@posting.google.com >, Old Wolf
<oldwolf@inspire.net.nz> writes[color=blue]
>Richard Herring <junk@[127.0.0.1]> wrote:[color=green]
>> Old Wolf writes:[color=darkred]
>> >"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote:
>> >>
>> >> Question: what is the type of !(d_flags & flag ).
>> >
>> >The results of && || ! are all int.[/color]
>>
>> Not according to ISO14882 sections 5.3.1, 5.14 and 5.15:
>> "The result is a bool".[/color]
>
>How right you are
>[color=green][color=darkred]
>> >> 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".[/color]
>>
>> I don't know why so many people have misgivings about the argument and
>> result types of !, && and ||.[/color]
>
>It's different in C.[/color]

It would have to be. C had no bool type.
[color=blue]
>But this is a different issue to int-to-bool
>conversions, which the OP was asking about.
>[color=green][color=darkred]
>> >false is zero and true is non-zero. This was the case
>> >even before computers were invented. No assembly instructions are
>> >required.[/color]
>>
>> There's at least one architecture where even=>false and odd=>true.[/color]
>
>Irrelevant to C++ (as was the original point, too)[/color]

You're the one who raised assembly instructions.

--
Richard Herring
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
When to check the return value of malloc Marty James answers 173 February 7th, 2008 12:05 AM
How to check version of an installed COM DLL jwexqm answers 2 November 15th, 2005 02:52 PM
is (!ptr) or (ptr) valid way to check for NULL or NOT NULL? G Fernandes answers 9 November 14th, 2005 07:38 PM
Check if $_GET["menu"] is null. Nuno Paquete answers 32 July 17th, 2005 08:41 AM