-2

Can't find a reference to this anywhere -- I have the following code I'm trying to work with and can anyone please tell me what the !! means in this context? Just to be clear it's "bang bang" !! not "pipe pipe" ||. Thanks in advance!

/* Branch prediction */
#ifdef __GNUC__
# define likely(p)     __builtin_expect(!!(p), 1)
# define unlikely(p)   __builtin_expect(!!(p), 0)
# define unreachable() __builtin_unreachable()
#else
# define likely(p)     (!!(p))
# define unlikely(p)   (!!(p))
# define unreachable() ((void)0)
#endif
GR99
  • 165
  • 8

2 Answers2

0

! is a simple boolean "Not" operation.

You're probably familiar with it as in:

if (!x)  // If x is 0, then execute the if-statement.

!! is two of those chained together, which has the effect of taking any non-zero value and converting it to 1/true, and any zero value stays zero/false


It is used in certain macros where a 1-or-0 value is needed, or when you want to convert an arbitrary value (pointer, double, char, etc) to a pure 1-or-0 boolean value.
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

The ! operator performs the logical NOT operation. If it's operand is 0 the result is 1, and if the operand is non-zero the result is 0.

So !! is the logical NOT operator applied twice. So what does this do? If the operand is 0, the inner ! converts it to 1, then the outer ! converts that to 0. If the operand is non-zero, the inner ! converts it to 0, then the outer ! converts that to 1.

So !! converts a value to its boolean equivalent. If the value is 0, it remains 0, otherwise it becomes 1.

dbush
  • 205,898
  • 23
  • 218
  • 273