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

# 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

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

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.

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

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

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

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.

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

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

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

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

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