What's the tilde in a &= ~b ?

Discussion in 'C++' started by Benjamin B., Apr 4, 2006.

  1. Benjamin B.

    Benjamin B. Guest

    Hi everyone,

    in this thing I inherited there's a statement like this:

    a &= ~b;

    where a is an int and b has been declared this way:

    const b = 0x0002;

    and thus is probably an int as well.

    My question: What's the tilde doing there? Is that standard C++? What
    does it mean?

    Cheers &= thanks
    Benjamin
     
    Benjamin B., Apr 4, 2006
    #1
    1. Advertising

  2. Benjamin B.

    Howard Guest

    "Benjamin B." <> wrote in message
    news:e0u1e8$380$...
    > Hi everyone,
    >
    > in this thing I inherited there's a statement like this:
    >
    > a &= ~b;
    >
    > where a is an int and b has been declared this way:
    >
    > const b = 0x0002;
    >
    > and thus is probably an int as well.


    (If I recall, implicit int's are no longer used. It should specify const
    int, for clarity if nothing else.)

    >
    > My question: What's the tilde doing there? Is that standard C++? What
    > does it mean?
    >



    That's the "bitwise complement" operator. It reverses the bits (1s become
    0s, and vice-versa). (You really should get yourself a good C++ book, by
    the way.)

    -Howard
     
    Howard, Apr 4, 2006
    #2
    1. Advertising

  3. Benjamin B.

    Noah Roberts Guest

    Benjamin B. wrote:
    > Hi everyone,
    >
    > in this thing I inherited there's a statement like this:
    >
    > a &= ~b;
    >
    > where a is an int and b has been declared this way:
    >
    > const b = 0x0002;
    >
    > and thus is probably an int as well.
    >
    > My question: What's the tilde doing there? Is that standard C++? What
    > does it mean?


    If you are familiar with gates, which all these bitwise operators are,
    it is NOT. NOT inverts its input so when it has power it is off, when
    it doesn't it is on. Power is 1, no power is 0. Output is 0 where
    there is a 1 and 1 where there is a 0 in the input.
     
    Noah Roberts, Apr 4, 2006
    #3
  4. Benjamin B.

    Benjamin B. Guest

    Howard wrote:
    > "Benjamin B." <> wrote in message
    > news:e0u1e8$380$...
    >> Hi everyone,
    >>
    >> in this thing I inherited there's a statement like this:
    >>
    >> a &= ~b;
    >>
    >> where a is an int and b has been declared this way:
    >>
    >> const b = 0x0002;
    >>
    >> and thus is probably an int as well.

    >
    > (If I recall, implicit int's are no longer used. It should specify const
    > int, for clarity if nothing else.)


    Sure it should. It's on the todo list, there are more of those.

    >> My question: What's the tilde doing there? Is that standard C++? What
    >> does it mean?

    >
    > That's the "bitwise complement" operator. It reverses the bits (1s become
    > 0s, and vice-versa).


    Thanks a lot!

    > (You really should get yourself a good C++ book, by
    > the way.)


    Uhm possible ... though I'll rather concentrate on the standard library
    and the likes if I have the choice ...

    Cheers
    Benjamin
     
    Benjamin B., Apr 4, 2006
    #4
  5. Benjamin B.

    Henryk Guest

    This operation is often used to clear a bit / flag in some kind of
    status variables.

    In your example the ~ operator (bitwise complement) sets all bits to 1
    except the bit that corresponds to 0x0020.

    Then the & operator (AND) will unset only the bit 0x0020 in your
    variable a since this is the only bit set to 0.

    Henryk
     
    Henryk, Apr 4, 2006
    #5
  6. Benjamin B.

    Ben Pope Guest

    Noah Roberts wrote:
    > Benjamin B. wrote:
    >> Hi everyone,
    >>
    >> in this thing I inherited there's a statement like this:
    >>
    >> a &= ~b;
    >>
    >> where a is an int and b has been declared this way:
    >>
    >> const b = 0x0002;
    >>
    >> and thus is probably an int as well.
    >>
    >> My question: What's the tilde doing there? Is that standard C++? What
    >> does it mean?

    >
    > If you are familiar with gates, which all these bitwise operators are,
    > it is NOT. NOT inverts its input so when it has power it is off, when
    > it doesn't it is on. Power is 1, no power is 0. Output is 0 where
    > there is a 1 and 1 where there is a 0 in the input.


    I think you are talking about ! (logical NOT), it is in the same family as:
    &&
    ||
    ==
    !=
    <
    >

    <=
    >=


    operator~ is bitwise NOT or complement. It applies a NOT to each and
    every bit, it is in the same family as:
    &
    |
    ^
    <<
    >>


    For unsigned integers, at least.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Apr 4, 2006
    #6
  7. Benjamin B.

    Noah Roberts Guest

    Ben Pope wrote:
    > Noah Roberts wrote:
    > > Benjamin B. wrote:
    > >> Hi everyone,
    > >>
    > >> in this thing I inherited there's a statement like this:
    > >>
    > >> a &= ~b;
    > >>
    > >> where a is an int and b has been declared this way:
    > >>
    > >> const b = 0x0002;
    > >>
    > >> and thus is probably an int as well.
    > >>
    > >> My question: What's the tilde doing there? Is that standard C++? What
    > >> does it mean?

    > >
    > > If you are familiar with gates, which all these bitwise operators are,
    > > it is NOT. NOT inverts its input so when it has power it is off, when
    > > it doesn't it is on. Power is 1, no power is 0. Output is 0 where
    > > there is a 1 and 1 where there is a 0 in the input.

    >
    > I think you are talking about ! (logical NOT), it is in the same family as:
    > &&
    > ||
    > ==
    > !=
    > <
    > >

    > <=
    > >=


    No, but those are boolean operators that behave in similar ways. If
    you wish to think of them in those terms as well it might help. At
    least &&, ||, and ! may very well be implemented as gates.
    >
    > operator~ is bitwise NOT or complement. It applies a NOT to each and
    > every bit, it is in the same family as:
    > &


    AND

    > |


    OR

    > ^


    NAND

    > <<
    > >>


    I can't think of any gate that will perform these functions.
     
    Noah Roberts, Apr 4, 2006
    #7
  8. Benjamin B.

    Howard Guest

    "Benjamin B." <> wrote in message
    news:e0u3ct$66q$...
    > Howard wrote:


    >> (You really should get yourself a good C++ book, by
    >> the way.)

    >
    > Uhm possible ... though I'll rather concentrate on the standard library
    > and the likes if I have the choice ...
    >


    You lost me there. What do you mean by the "standard library"? The
    "standard template library" [STL]? The STL is a set of templates, written
    in C++. If you're referring to the C++ run-time library, then you're
    talking about a given compiler vendor's implementation of the C++ language
    features (such as I/O, basic math functions, etc.). Perhaps you're talking
    about some on-line Help system in your compiler's IDE?

    In any case, you need some source of information which will tell you how to
    program in C++, what its basic syntax and rules are, and what the language
    features are and how to use them. Sounds like a book to me... you got some
    other way to learn all that?

    -Howard
     
    Howard, Apr 4, 2006
    #8
  9. Hello,

    Noah Roberts wrote:

    >> ^

    >
    > NAND


    ^ is bitwise XOR, not a bitwise NAND

    >
    >> <<
    >> >>

    >
    > I can't think of any gate that will perform these functions.


    If you take wiring and a constant zero bit...

    Bernd
     
    Bernd Strieder, Apr 4, 2006
    #9
  10. Howard wrote:
    >> >> (You really should get yourself a good C++ book, by
    > >> the way.)

    > >
    > > Uhm possible ... though I'll rather concentrate on the standard library
    > > and the likes if I have the choice ...
    > >

    >
    > You lost me there. What do you mean by the "standard library"? The
    > "standard template library" [STL]? The STL is a set of templates, written
    > in C++. If you're referring to the C++ run-time library, then you're
    > talking about a given compiler vendor's implementation of the C++ language
    > features (such as I/O, basic math functions, etc.). Perhaps you're talking
    > about some on-line Help system in your compiler's IDE?


    Have you really not heard of the C++ standard library? If not, perhaps
    you need to follow your own good advice and get yourself a good book as
    well. I highly recommend Josuttis' The C++ Standard Library. Or
    perhaps I misunderstood you (I seem to do that at times).

    Best regards,

    Tom
     
    Thomas Tutone, Apr 4, 2006
    #10
  11. Benjamin B.

    Howard Guest

    "Thomas Tutone" <> wrote in message
    news:...
    >
    > Howard wrote:
    >>> >> (You really should get yourself a good C++ book, by
    >> >> the way.)
    >> >
    >> > Uhm possible ... though I'll rather concentrate on the standard library
    >> > and the likes if I have the choice ...
    >> >

    >>
    >> You lost me there. What do you mean by the "standard library"? The
    >> "standard template library" [STL]? The STL is a set of templates,
    >> written
    >> in C++. If you're referring to the C++ run-time library, then you're
    >> talking about a given compiler vendor's implementation of the C++
    >> language
    >> features (such as I/O, basic math functions, etc.). Perhaps you're
    >> talking
    >> about some on-line Help system in your compiler's IDE?

    >
    > Have you really not heard of the C++ standard library? If not, perhaps
    > you need to follow your own good advice and get yourself a good book as
    > well. I highly recommend Josuttis' The C++ Standard Library. Or
    > perhaps I misunderstood you (I seem to do that at times).
    >


    I see your point. My book (The C++ Programming Language", Stroustrup),
    refers to the "standard library", but it's kind of talking about everything
    in the C++ language, including the STL. (In fact, in answering his own
    question: "what should be in the standard library?", he answers:
    "everything!".) So really, saying the "standard library" doesn't mean all
    that much, does it? Mostly people talk about the STL, otherwise they're
    really just talking about the language as a whole, right?

    And anyway, my point was really: what about using the "standard library"
    negates needing a book to study it? The OP was saying he'd "rather
    concentrate on the standard library..." when I suggested getting a C++ book,
    as if one precluded the other. That statement is really what made no sense
    to me.

    -Howard
     
    Howard, Apr 4, 2006
    #11
  12. Benjamin B.

    Rolf Magnus Guest

    Howard wrote:

    >>> You lost me there. What do you mean by the "standard library"? The
    >>> "standard template library" [STL]? The STL is a set of templates,
    >>> written in C++. If you're referring to the C++ run-time library, then
    >>> you're talking about a given compiler vendor's implementation of the C++
    >>> language features (such as I/O, basic math functions, etc.). Perhaps
    >>> you're talking about some on-line Help system in your compiler's IDE?

    >>
    >> Have you really not heard of the C++ standard library? If not, perhaps
    >> you need to follow your own good advice and get yourself a good book as
    >> well. I highly recommend Josuttis' The C++ Standard Library. Or
    >> perhaps I misunderstood you (I seem to do that at times).
    >>

    >
    > I see your point. My book (The C++ Programming Language", Stroustrup),
    > refers to the "standard library", but it's kind of talking about
    > everything in the C++ language, including the STL.


    Maybe by "STL" you do mean the standard library, because the C++ standard
    library is AFAIK based on the STL to a great degree.

    > (In fact, in answering his own question: "what should be in the standard
    > library?", he answers: "everything!".) So really, saying the "standard
    > library" doesn't mean all that much, does it?


    Well, what should be and what is in the standard library are two different
    things.

    > Mostly people talk about the STL, otherwise they're really just talking
    > about the language as a whole, right?


    If people here talk about "the standard library", they usually mean the
    library that is part of standard C++, i.e. defined in the C++ standard.

    > And anyway, my point was really: what about using the "standard library"
    > negates needing a book to study it? The OP was saying he'd "rather
    > concentrate on the standard library..." when I suggested getting a C++
    > book, as if one precluded the other. That statement is really what made
    > no sense to me.


    Right. It doesn't make much sense to get all the details about the C++
    standard library without even knowing the basics about the C++ syntax.
     
    Rolf Magnus, Apr 4, 2006
    #12
  13. Benjamin B.

    Mike Smith Guest

    Benjamin B. wrote:
    > Howard wrote:
    >> (You really should get yourself a good C++ book, by
    >> the way.)

    >
    > Uhm possible ... though I'll rather concentrate on the standard library
    > and the likes if I have the choice ...


    You still need to learn the *language*.

    --
    Mike Smith
     
    Mike Smith, Apr 4, 2006
    #13
    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. SStory

    Tilde for relative path

    SStory, Oct 16, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,796
    SStory
    Oct 19, 2003
  2. CHristian Niss
    Replies:
    0
    Views:
    493
    CHristian Niss
    Oct 31, 2003
  3. =?Utf-8?B?QWxleCBNYWdoZW4=?=

    Use of Tilde (~) and <LINK>

    =?Utf-8?B?QWxleCBNYWdoZW4=?=, May 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    5,518
    =?Utf-8?B?QWxleCBNYWdoZW4=?=
    May 11, 2004
  4. MB2

    Tilde Expansion

    MB2, Sep 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    454
    Rick Strahl [MVP]
    Sep 24, 2004
  5. =?Utf-8?B?U2hhd24=?=

    Problems with Tilde in Microsoft Development Model

    =?Utf-8?B?U2hhd24=?=, Dec 24, 2004, in forum: ASP .Net
    Replies:
    10
    Views:
    678
    =?Utf-8?B?U2hhd24=?=
    Jan 5, 2005
Loading...

Share This Page