Connecting Tech Pros Worldwide Help | Site Map

How to check if a bit is off?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:00 PM
Siemel Naran
Guest
 
Posts: n/a
Default 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;




  #2  
Old July 22nd, 2005, 06:00 PM
Gianni Mariani
Guest
 
Posts: n/a
Default 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, 06:00 PM
Siemel Naran
Guest
 
Posts: n/a
Default 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, 06:00 PM
Risto Lankinen
Guest
 
Posts: n/a
Default 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, 06:00 PM
Niels Dybdahl
Guest
 
Posts: n/a
Default 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, 06:02 PM
Old Wolf
Guest
 
Posts: n/a
Default 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, 06:04 PM
Richard Herring
Guest
 
Posts: n/a
Default 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, 06:08 PM
Old Wolf
Guest
 
Posts: n/a
Default 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, 06:08 PM
Richard Herring
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.