"Not"
Author |
Message |
rar
![](http://compsci.ca/v3/uploads/user_avatars/14728088994c2a2a0ec03c0.gif)
|
Posted: Wed Oct 07, 2009 7:41 pm Post subject: "Not" |
|
|
What is the difference between '~' and '!' in C?
I've been reviewing my notes for an upcoming quiz and was not aware there was a difference... |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
saltpro15
![](http://compsci.ca/v3/uploads/user_avatars/9776171634ced6278d14c2.png)
|
Posted: Wed Oct 07, 2009 7:49 pm Post subject: RE:"Not" |
|
|
~ can be used as a destructor right?
like
|
|
|
|
|
![](images/spacer.gif) |
bbi5291
|
Posted: Wed Oct 07, 2009 7:52 pm Post subject: Re: "Not" |
|
|
The ! operator is a logical negation. Applied to zero (false), it yields one (true). Applied to any nonzero value (true), it yields zero (false).
The ~ operator on the other hand is a bitwise negation. When ~ is applied to an integer, every bit in the integer's binary representation is flipped. |
|
|
|
|
![](images/spacer.gif) |
DtY
![](http://compsci.ca/v3/uploads/user_avatars/8576159234be48b7a8b0e8.png)
|
Posted: Wed Oct 07, 2009 7:54 pm Post subject: RE:"Not" |
|
|
! is logical, ~ is bitwise, ! is the not of || and &&, ~ is the not of | and &.
For example, (I don't know if 0b works in C, but I'll assume it does, it means binary number, assume 8 bit)
!0b00000001 = 0b00000000
~0b00000001 = 0b11111110
!0b00000000 = 0b00000001
~0b00000000 = 0b11111111 |
|
|
|
|
![](images/spacer.gif) |
|
|