Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "!!" in C? [duplicate]

I have encountered the following snippet:

pt->aa[!!(ts->flags & MASK)] = -val; 
  1. What does !! (double exclamation marks/ exclamation points/ two NOT operators) stand for in c?
  2. Doesn't (!!NULL) == NULL?
like image 849
0x90 Avatar asked Feb 07 '13 13:02

0x90


People also ask

What is duplicate elements in C?

In this program, we need to print the duplicate elements present in the array. This can be done through two loops. The first loop will select an element and the second loop will iteration through the array by comparing the selected element with other elements.

What is a duplicate element?

Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.

How do I get rid of duplicate elements?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.

Can set have duplicate values C?

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.


2 Answers

! is negation. So !! is negation of negation. What is important is the fact that the result will be an int.

  • !!x if x == 0 is !!0, that is !1, that is 0.
  • !!x if x != 0 is !!(!0), that is !!1, that is !0, that is 1.

!! is used commonly if you want to convert any non-zero value to 1 while being certain that 0 remains a 0.

And indeed, !!NULL == NULL, since !!NULL == !!0 and !!0 == !1 and finally !1 == 0.

Consequently, in the short piece of code you cited the array subscript will be either 0 if the value of the expression in parenthesis is NULL, and 1 otherwise.

like image 77
Jan Avatar answered Sep 16 '22 13:09

Jan


It is commonly (ab)used to convert any value into the ints 0 or 1 by repeated application of the boolean not operator, !.

For instance: !56 is 0, since 56 is "true" when viewed as a boolean. This means that !!56 is 1, since !0 is 1.

like image 28
unwind Avatar answered Sep 20 '22 13:09

unwind