07-27-2023, 11:49 AM
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.
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.