How to check if a bit is off?

Discussion in 'C++' started by Siemel Naran, Aug 18, 2004.

  1. Siemel Naran

    Siemel Naran Guest

    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;
     
    Siemel Naran, Aug 18, 2004
    #1
    1. Advertising

  2. 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.
     
    Gianni Mariani, Aug 18, 2004
    #2
    1. Advertising

  3. Siemel Naran

    Siemel Naran Guest

    "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?
     
    Siemel Naran, Aug 18, 2004
    #3
  4. "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 -
     
    Risto Lankinen, Aug 18, 2004
    #4
  5. > 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
     
    Niels Dybdahl, Aug 18, 2004
    #5
  6. Siemel Naran

    Old Wolf Guest

    "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.
     
    Old Wolf, Aug 18, 2004
    #6
  7. 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
     
    Richard Herring, Aug 20, 2004
    #7
  8. Siemel Naran

    Old Wolf Guest

    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)
     
    Old Wolf, Aug 24, 2004
    #8
  9. 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
     
    Richard Herring, Aug 24, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    0
    Views:
    477
    Steve
    Jan 31, 2006
  2. Replies:
    3
    Views:
    1,859
    Timothy Bendfelt
    Jan 19, 2007
  3. Replies:
    9
    Views:
    1,056
    Juha Nieminen
    Aug 22, 2007
  4. Jeff.M
    Replies:
    6
    Views:
    204
    Lasse Reichstein Nielsen
    May 4, 2009
  5. prati
    Replies:
    0
    Views:
    502
    prati
    Oct 27, 2012
Loading...

Share This Page