Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 407 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
!! c operator, is a two NOT?

#1
I reading this [code](

[To see links please register here]

), and have this line

switch (!!up + !!left) {

what is `!!` operator ? two logical NOT ?
Reply

#2
yes, it's two nots.

`!!a` is `1` if `a` is non-zero and `0` if `a` is `0`

You can think of `!!` as clamping, as it were, to `{0,1}`. I personally find the usage a bad attempt to appear fancy.
Reply

#3
You're right. It's two nots. To see why one would do this, try this code:

#include <stdio.h>

int foo(const int a)
{
return !!a;
}

int main()
{
const int b = foo(7);
printf(
"The boolean value is %d, "
"where 1 means true and 0 means false.\n",
b
);
return 0;
}

It outputs `The boolean value is 1, where 1 means true and 0 means false.` If you drop the `!!`, though, it outputs `The boolean value is 7, where 1 means true and 0 means false.`
Reply

#4
You can imagine it like this:

!(!(a))

If you do it step by step, this make sense

result = !42; //Result = 0
result = !(!42) //Result = 1 because !0 = 1

This will return `1` with any number (-42, 4.2f, etc.) but only with `0`, this will happens

result = !0; //Result = 1
result = !(!0) //result = 0
Reply

#5
`!!` is a more-portable (pre-C99) alternative to `(_Bool)`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through