Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
unity3017yIf I used bit shifting in a PR my lead would be like "Super cool dude... Rejected."
I'm sure there are certain companies where it would be fine in certain optimization circumstances, but in general I think you're right. Readable and concise: Math.pow or even i * i would usually be preferred. -
It depends on the company and who is reviewing the code, and also how performance critical the application is.
-
ddephor45117yPerformance is not an issue in this case, because a modern compiler will know better than you what should be used and optimize it automatically for the given target.
You often hear that ressource bullshit, but you can be sure that the usual dev is not smarter than a modern compiler. Sometimes such premature "human" optimization even prevent compiler optimization strategies, so it may even make it worse.
The readability depends on the situation. If you're calculating something, use math.pow, if you're bit-fiddling, use 1 << n. -
Here’s a fun exercise for you now that you know this.
Write an algorithm to get the value of the nth bit of a given byte (or really any data type). -
Dafuq. Everyone rejecting it should go home.
Imho 1 << n is way more readable. What is a review for if the code isn't read and understood?
BUT i guess, too, the compiler is doing this anyway. And secondly, how often do you need powers of 2 in your *average* application? -
ddephor45117y@daintycode I need powers of 2 regularly. Usually not for calculating, but for creating bit masks or bit strings on the fly, where having constants or lookup tables make no sense.
Related Rants
-
linuxxx32*client calls in* Me: good morning, how can I help you? Client: my ip is blocked, could you unblock it for m...
-
DRSDavidSoft28Found this in our codebase, apparently one of my co-workers had written this
-
linuxxx23*client calls* "hello, we forgot the password to our WiFi router. Could you reset that for us?" 😐😶😮...
We just learned in university that you should use '1 << i' instead of 'Math.pow(2, i)' because it is faster when calculating 2^i...
Am I the only one who thinks that this would never get through a code review in a company? I mean it is harder to realize what is going on especially when you didn't went to university lately.
rant
wtf