3 ms·
In the computer games industry, or at least at the companies I have worked in based in the UK, these would be part of a test given either remotely or immediatel
by scraft 8y ago
In the computer games industry, or at least at the companies I have worked in based in the UK, these would be part of a test given either remotely or immediately on arrival and failing to answer correctly would make it unlikely to proceed any further.
In the c/c++ game code bases I have worked on, it is very common to have statuses stored as a uint with each bit representing something and very common to set, unset, toggle, reverse bits etc.
I actually read the first part of the article thinking these aren't hacks these are just things people use day in day out. But the later section about setting last 0 or 1 bit etc suddenly because things I wouldn't typically see in the wild.
- majewsky 8y agoAt least in C++, wouldn't you just write these "hacks" only once, in inline methods of a BitField class? It's much more likely to mistype state = state & ~BIT7; than to mistype state = state.without(BIT7);
- dahfizz 8y agoWould you create a wrapper class and member function to make addition less likely to mistype? num = num.plus(x); might be less likely to mistype than num = num + x; but nobody would ever do that. Once you've done bitwise operators a few times they become just as comfortable and natural as addition or subtraction.
- daemin 8y agoNope, people actually just write the bit setting code. It's simpler and also I haven't found a good generic bit flags class that works as you intend.
- slededit 8y agoWhat about C++ bitfields? Not a library but a language extension. A lot of code I used has migrated to that.
- daemin 8y agoNormal C++ bitfields? Sometimes you get that inside classes in the form of "bool x : 1; bool y : 1;" etc. But that's much harder to use as far as function parameters go, so old school bitmasks are used for that.
- scraft 8y agoAs daemin says, no, I am used to working with people that find basic bit field manipulation easy to work with[1], so there isn't any confusion. Plus there are performance concerns. I remember one graduate introducing a c++ bit field class and tried to replace some core functionality with it and killed performance. Developers prefer to debug non optimized builds, and non optimized builds can end up running slow when you replace simple one liners with classes and inline methods that don't get optimized in debug builds. [1]I hope this doesn't sound aggressive as it really isn't intended to. It is just everyone is so used to seeing bitwise operations it is as straight forwards as reading "set the third bit" or "toggle the last bit" when we see the C code.
- coldacid 8y agoI'd only do that if I could force every compiler to _always_ inline those methods. The cost of a method call for a simple bit operation is magnitudes greater than just doing the operation itself (tens to hundreds of ops versus 1-5).