> true which expands to the integer constant 1,
And equality yields a 1 or 0. C11 §6.5.9 (3):
> The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence. Each of the operators yields 1 if the specified relation is true and 0 if it is false.
Here’s a thing you could do (but I don’t know why):
+= !(c-’s’) - !(c-’p’)
int num_empty_strings = !!(strlen(s1)) + !!(strlen(s2)) + !!(strlen(s3))
which is equivalent to: int num_empty_strings = (strlen(s1) != 0) + (strlen(s2) != 0) + (strlen(s3) != 0)
Which you use is really a matter of coding style. int num_empty_strings = !!*s1 + !!*s2 + !!*s3;One thought: If the code is rewritten using bit arithmetic, then potentially the result could be even faster as there need not be a pointer look-up.
A bit arithmetic solution would have a mask generated for the characters ‘p’ and ‘s’, and then the result could be AND-ed, and then with more bit arithmetic this all 1s value can be translated to a 1 if and only if all the bits are 1. Following which, there would be a no conditional check and simply be both an add and subtract operations but where the value to be added will only be 1 if the mask for ‘p’ matches and 1 if the mask for ‘s’ matches. I’m not fully sure if this would necessary be faster than the pointer look-up solution, but it would be interested to try this version of the code and see how fast it performs.
https://owen.cafe/posts/the-same-speed-as-c/
And as others have pointed out, you can tweak the input, then vectorize the algo, if you want to go that route.
I considered this a pedagogical exercise and I sincerely hope nobody will start dropping down to assembly without a very good reason to.