Hacker News new | ask | show | jobs
by __david__ 2555 days ago
For the same reason that this:

    if (c)
        printf("Hello");
        exit(0);
    exit(1);
Produces a warning in gcc:

    test.c:7:5: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
         if (c)
         ^~
    test.c:9:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
             exit(0);
             ^~~~
"2^32" is of course just an xor, but it makes no sense (the "exclusive" part of it is not being exercised). If you really mean 2 bits being set in a constant then "2 | 32" is much clearer. If you're dead set on xor, "32^2" won't trigger the warning.
1 comments

It's not for you or the compiler to decide that 2^32 makes no sense. It is using ^ exactly as intended and is neither misleading nor ambiguous.

Stating that 32^2 should not trigger a warning while 2^32 should shows that this proposal has not been thought through.

It's not desirable or sensible to raise a warning on the premise that the expression might mean something else in another language, which is what this would do.

I feel perfectly qualified to say that 2^32 makes no sense.

Just because something is specced doesn't mean that it's reasonable. My "if statement" example is perfectly valid C, but it's not reasonable. 2^32 is perfectly valid, but also unreasonable. Frankly, any bitwise operations with >10 decimal constants are either intentionally obfuscating, or done by someone who doesn't know what they are doing. Consider "2^32" vs "0x02^0x20". The latter is much better.

It is entirely sensible to warn on the premise that it might mean something in another language: The construct seems to be an actual point of confusion (the authors sited numerous real code examples where people are accidentally doing this in the wild), and the construct makes no sense at face value. I think you'd be hard pressed to find a real example of 2^32 in code where it isn't a bug. That's enough to make it a reasonable warning.