Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What's the tilde in a &= ~b ?

Reply
Thread Tools

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

 
 
Benjamin B.
Guest
Posts: n/a
 
      04-04-2006
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
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      04-04-2006

"Benjamin B." <(E-Mail Removed)> wrote in message
news:e0u1e8$380$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      04-04-2006

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.

 
Reply With Quote
 
Benjamin B.
Guest
Posts: n/a
 
      04-04-2006
Howard wrote:
> "Benjamin B." <(E-Mail Removed)> wrote in message
> news:e0u1e8$380$(E-Mail Removed)...
>> 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
 
Reply With Quote
 
Henryk
Guest
Posts: n/a
 
      04-04-2006
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

 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      04-04-2006
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...
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      04-04-2006

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.

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      04-04-2006

"Benjamin B." <(E-Mail Removed)> wrote in message
news:e0u3ct$66q$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Bernd Strieder
Guest
Posts: n/a
 
      04-04-2006
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

 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      04-04-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with Tilde in Microsoft Development Model =?Utf-8?B?U2hhd24=?= ASP .Net 10 01-05-2005 03:07 PM
Tilde Expansion MB2 ASP .Net 1 09-24-2004 08:45 PM
Use of Tilde (~) and <LINK> =?Utf-8?B?QWxleCBNYWdoZW4=?= ASP .Net 2 05-10-2004 11:06 PM
tilde-replacement changes from framework 1.0 to 1.1 ???? CHristian Niss ASP .Net 0 10-31-2003 09:19 AM
Tilde for relative path SStory ASP .Net 2 10-19-2003 12:45 AM



Advertisments