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

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

1. ### 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

2. ### HowardGuest

"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

3. ### Noah RobertsGuest

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
4. ### 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
5. ### HenrykGuest

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
6. ### Ben PopeGuest

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
7. ### Noah RobertsGuest

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
8. ### HowardGuest

"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

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
9. ### Bernd StriederGuest

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
10. ### Thomas TutoneGuest

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

Have you really not heard of the C++ standard library? If not, perhaps
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
11. ### HowardGuest

"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

>
> Have you really not heard of the C++ standard library? If not, perhaps
> 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
12. ### Rolf MagnusGuest

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
>> 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
13. ### Mike SmithGuest

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