7

Given that x is a variable of type int with the number 5 as its value, consider the following statement:

int y = !!x;

This is what I think it happens: x is implicitly casted to a bool and the first negation is executed, after that the last negation is made, so a cast and two negations.

My question is, isn't just casting to bool (executing int y = (bool)x; instead of int y = !!x) faster than using double negation, as you are saving two negations from executing.

I might be wrong because I see the double negation a lot in the Linux kernel, but I don't understand where my intuition goes wrong, maybe you can help me out.

3
  • 5
    There was no bool in C when the Linux kernel was first written.
    – jwdonahue
    Jul 6, 2018 at 23:22
  • 2
    Bool me twice, shame on me.
    – jxh
    Jul 6, 2018 at 23:32
  • 2
    There is no such thing as “implicitly casted”. A cast is a syntactic construct. x could have been implicitly converted to bool, but that's not what happens in !x, which has type int, and in !!x, which also has type int. Jul 7, 2018 at 0:49

3 Answers 3

10

There was no bool type when Linux was first written. The C language treated everything that was not zero as true in Boolean expressions. So 7, -2 and 0xFF are all "true". No bool type to cast to. The double negation trick ensures the result is either zero or whatever bit pattern the compiler writers chose to represent true in Boolean expressions. When you're debugging code and looking at memory and register values, it's easier to recognize true values when they all have the same bit patterns.

Addendum: According the C89 draft standard, section 3.3.3.3:

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int . The expression !E is equivalent to (0==E).

So while there was no Boolean type in the early days of the Linux OS, the double negation would have yielded either a 0 or a 1 (thanks to Gox for pointing this out), depending on the truthiness of the expression. In other words any bit pattern in the range of INT_MIN..-1 and 1..INT_MAX would have yielded a 1 and the zero bit pattern is self-explanatory.

1
  • @Ivan, thanks for the edit, my lesdixya must have kicked in, because I clearly remember looking at the <stdint.h> file before writing those constants! ;(
    – jwdonahue
    Jul 14, 2018 at 4:39
1

C language unlike other languages does not have bool type. bool in C is actually defined in stdbool.h which is not included in many C projects. Linux kernel is one such projects, it would be a pain to go through Linux code and update everything to use bool now as well. That is reason why Linux kernel does not use bool in C.

why !!x? This is done to ensure that value of y is either 1 or 0. As an example if you have this cocd

x=5;
int y = !!x;

We know that everything that non-zero values in C mean true. So above code would brake down to y= !!(5) followed by y = !(0) and than y = 1.

EDIT: One more thing, I just saw OP mentioned casting to bool. In C there is no bool as base type, bool is defined type, thus compilers do not cast integers to Boolean type.

EDIT 2: To further explain, in C++, Java and other languages when you type bool a = false you do not have to use headers or compile some other libraries or define bool type for bool type to work, it is already incorporated into compilers, where as in c you have to.

EDIT 3: bool is not the same as _Bool.

15
  • 1
    It may be true that 1 represents !false on many systems, but I am pretty sure that the only portable assumption you could make (prior to C99?), was that zero was false and non-zero was true. The compiler writers were free to use whatever bit pattern they liked for true and had to accept any non-zero value as true.
    – jwdonahue
    Jul 6, 2018 at 23:43
  • 3
    @jwdonahue !false == 1 has been guaranteed since standardization at least (C89), so "many systems" includes all standard conforming ones.
    – eerorika
    Jul 6, 2018 at 23:51
  • @user2079303 could be right, but all I know that gcc and clang guaranty !0 = 1. I cannot find examples of exact code, but in Linux kernel you would often see this if (x == 1) meaning x is true.
    – user5550963
    Jul 6, 2018 at 23:55
  • 1
    "C language [...] does not have bool type" Well, not anymore. C99 introduced _Bool, which is intrinsic.
    – alk
    Jul 7, 2018 at 6:20
  • 2
    The relevant part of the C11 standard can be read up here: port70.net/~nsz/c/c11/n1570.html#6.7.2
    – alk
    Jul 11, 2018 at 14:22
0

The only reason I can imagine is because this saves some typing (7 chars vs 2 chars).

As @jwdonahue and @Gox have already mentioned, this is not the correct reason. C did not have bool when the linux kernel was written therefore casting to bool was not an option.

As far as efficiency goes, both are equivalent because compilers can easily figure this out. See https://godbolt.org/g/ySo6K1

bool cast_to_bool_1(int x) {
    return !!x;
}

bool cast_to_bool_2(int x) {
    return (bool) x;
}

Both the functions compile to the same assembly which uses the test instruction to check if the argument is zero or not.

test edi, edi   // checks if the passed argument is 0 or not
setne al        // set al to 0 or 1 based on the previous comparison
ret             // returns the result
1
  • 2
    It's only required to use 7 chars if you are using _Bool instead of bool. But, bool requires #include <stdbool.h>.
    – jxh
    Jul 6, 2018 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.