473,394 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

What's the tilde in a &= ~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
Apr 4 '06 #1
12 17958

"Benjamin B." <no*****@today.com> wrote in message
news:e0**********@online.de...
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
Apr 4 '06 #2

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.

Apr 4 '06 #3
Howard wrote:
"Benjamin B." <no*****@today.com> wrote in message
news:e0**********@online.de...
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
Apr 4 '06 #4
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

Apr 4 '06 #5
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...
Apr 4 '06 #6

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.

Apr 4 '06 #7

"Benjamin B." <no*****@today.com> wrote in message
news:e0**********@online.de...
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
Apr 4 '06 #8

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

Apr 4 '06 #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

Apr 4 '06 #10

"Thomas Tutone" <Th***********@yahoo.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...

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



Apr 4 '06 #11
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.

Apr 4 '06 #12
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
Apr 4 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: CHristian Niss | last post by:
hi there, in a lot of user controls we are using "~" in server tags to reference objects like images <img src="~/images/something.gif" runat="server" />. with framework 1.0 the tilde got...
1
by: MB2 | last post by:
I have code in a User Control for an image button like this: <asp:ImageButton id="__0" runat="server" text="Delete" ImageUrl="~/Images/ImageLibrary/icon_delete.gif" /> This used to work and it...
10
by: Shawn | last post by:
Hi, For a few years, I have been developing each of my clients websites using a seperate web site (unique IP) to solve problems with relative URL's between my local dev station and the...
9
by: Jared Tullis | last post by:
We have an .NET 1.1 application running on 4 2K3 load balanced servers (using WLBS). IIS has the .NET aspnet_isapi.dll mapped as a wildcard application map. The web.config points *.html to a...
3
by: Karo | last post by:
I have troubles with tilde (~) character on my host. It gets replaced by server name instead of domain name from where redirect is coming from. Code...
1
by: Mythran | last post by:
In other web servers, more specifically apache and even non-web servers you can use the tilde character to specify the root directory, or home. Before I go reinventing the wheel (by parsing a...
4
by: ibiza | last post by:
Hi all, I have a problem which I don't understand with the "~" mark in a NavigateUrl property of an asp:HyperLink. I have this in a master page, to build the standard menu (rMenuItems is a...
2
by: jbolty | last post by:
There have been a few threads talking about using the tilde ~, to reference the webroot path. My problem though is handling an unexpected tilde in a url. this url: ...
3
by: John Nagle | last post by:
I have XML replies in a DOM which contain entity escapes, like "&amp;". What's the proper way to replace them with the ordinary characters? Preferably something that will work in most browsers? I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.