Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to check if a bit is off?

Reply
Thread Tools

How to check if a bit is off?

 
 
Siemel Naran
Guest
Posts: n/a
 
      08-18-2004
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;



 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-18-2004
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.
 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      08-18-2004
"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?


 
Reply With Quote
 
Risto Lankinen
Guest
Posts: n/a
 
      08-18-2004

"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 -


 
Reply With Quote
 
Niels Dybdahl
Guest
Posts: n/a
 
      08-18-2004
> 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


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      08-18-2004
"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.
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      08-20-2004
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
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      08-24-2004
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
conversions, which the OP was asking about.

> >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)
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      08-24-2004
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
>conversions, which the OP was asking about.
>
>> >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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Bit check using function check(var,pos) ((var)&(1<<pos)) prati C Programming 0 10-27-2012 05:25 PM
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57