How to check if a bit is off?

S

Siemel Naran

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;
 
G

Gianni Mariani

Siemel said:
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.
 
S

Siemel Naran

Gianni Mariani said:
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?
 
R

Risto Lankinen

Siemel Naran said:
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 -
 
N

Niels Dybdahl

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
 
O

Old Wolf

Siemel Naran said:
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.
 
R

Richard Herring

Old Wolf said:
The results of && || ! are all int.

Not according to ISO14882 sections 5.3.1, 5.14 and 5.15:
"The result is a bool".

Yes [ibid.]
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.
 
O

Old Wolf

Richard Herring said:
Not according to ISO14882 sections 5.3.1, 5.14 and 5.15:
"The result is a bool".

How right you are
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.
There's at least one architecture where even=>false and odd=>true.

Irrelevant to C++ (as was the original point, too)
 
R

Richard Herring

Old Wolf said:
How right you are


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.


Irrelevant to C++ (as was the original point, too)

You're the one who raised assembly instructions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top