4 ms·
I remember a recent example. What I wrote was: int x = somefunction(); int x_dividedby16 = x >> 4; My coworker corrected the second line to something
by kutkloon7 9y ago
I remember a recent example. What I wrote was:
int x = somefunction();
int x_dividedby16 = x >> 4;
My coworker corrected the second line to something like:
int x_dividedby16 = (int)Math.ceil(x / 16.0);
- biesnecker 9y agoDid your coworker correct it with "this is more readable" or "this is the correct way to do it"? The first is arguable (though I don't personally agree with the argument), the second just betrays a real lack of knowledge.
- sammoth 9y agoDon't the two bits of code have completely different behaviour? And the code with the bitshift is undefined on negative integers (in C). So the bottom code could indeed be the "correct way to do it".
- biesnecker 9y agoFair, with negative ints they're different. They have the same behavior on positive ints, though.
- krzat 9y agoWhy not `x / 16`?
- deleted 9y ago[deleted]
- seanwilson 9y agoI'd only ever use a bitshift if the intention of the code was to move the bits left or right (e.g. moving red, green or blue pixel component bits into the right position) or if it was absolutely required for speed as it's less readable if your intention is to divide a number. C compilers will easily optimise simple integer divisions like this for example.
- deleted 9y ago[deleted]