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

B

Benjamin B.

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
 
H

Howard

Benjamin B. said:
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
 
N

Noah Roberts

Benjamin said:
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.
 
B

Benjamin B.

Howard said:
(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.
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
 
H

Henryk

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
 
B

Ben Pope

Noah said:
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
 
N

Noah Roberts

Ben said:
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.
 
H

Howard

Benjamin B. said:
Howard wrote:

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
 
T

Thomas Tutone

Howard said:
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
 
H

Howard

Thomas Tutone said:
(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
 
R

Rolf Magnus

Howard said:
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.
 
M

Mike Smith

Benjamin said:
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*.
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top