I have encountered the following snippet:
pt->aa[!!(ts->flags & MASK)] = -val;
!!
(double exclamation marks/ exclamation points/ two NOT operators) stand for in c?(!!NULL) == NULL
?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.
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.
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.
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.
!
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.
It is commonly (ab)used to convert any value into the int
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With