Hacker News new | ask | show | jobs
by raxxorrax 2553 days ago
I don't know if it is a good practice, but I often have statements and expressions that look awfully similar. Flipping bits does happen often enough.

Mostly written as y ^ (1 << x), which could easily resolve to these expressions. Mostly at runtime, sure, but there are exceptions. Especially if you like descriptive constants. I would expect it to be quite difficult to separate the "correct cases" without people starting to just suppress compiler warnings or trick it with writing the same stuff in different words (which might be better).

On the other hand, setting max_short to something like 18 is probably a really god prank in larger programming environments. The compiler would just ruin all the fun here.

2 comments

> Mostly written as y ^ (1 << x), which could easily resolve to these expressions.

We’re talking about a parse-time check; things that resolve to those values after identifier binding won’t throw the warning. It’s not a warning about what you wrote (semantically), it’s a warning about how you wrote it (syntactically).

The point is very specifically cases where X is a decimal literal (not an expression such as 1 << x, or a hex number).

Things that resolve to this don't trigger the warning, and it is a compile time warning.

And then specifically looking at 2^X and 10^X occurring in source code.

I would agree with the suggestion then. It can happen that you just write it out like that and the compiler should maybe inform me. But why limit it to 2^ and 10^ and not say literal^literal should always produce a warning? Any common or valid usages that I am missing here?
Certainly, I'd say if either side is a hex or octal literal, it might be someone intentionally bit-twideling.

As for limiting the base, that is a more fluid matter. I guess you want to avoid warnings for very large numbers. Because those are more likely legitimate.

> But why limit it to 2^ and 10^ and not say literal^literal should always produce a warning?

You're suggesting that using the C language as specified and intended should produce a warning... That makes no sense.