By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,649 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

How to check if a bit is off?

P: 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;

Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.
Jul 22 '05 #2

P: n/a
"Gianni Mariani" <gi*******@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?
Jul 22 '05 #3

P: n/a

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:1f*********************@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 -
Jul 22 '05 #4

P: n/a
> 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
Jul 22 '05 #5

P: n/a
"Siemel Naran" <Si*********@REMOVE.att.net> wrote:
"Gianni Mariani" <gi*******@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.
Jul 22 '05 #6

P: n/a
In message <84**************************@posting.google.com >, Old Wolf
<ol*****@inspire.net.nz> writes
"Siemel Naran" <Si*********@REMOVE.att.net> wrote:
"Gianni Mariani" <gi*******@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
Jul 22 '05 #7

P: n/a
Richard Herring <ju**@[127.0.0.1]> wrote:
Old Wolf writes:
"Siemel Naran" <Si*********@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
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)
Jul 22 '05 #8

P: n/a
In message <84**************************@posting.google.com >, Old Wolf
<ol*****@inspire.net.nz> writes
Richard Herring <ju**@[127.0.0.1]> wrote:
Old Wolf writes:
>"Siemel Naran" <Si*********@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
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
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.