Does anyone use bitwise operators anymore?

Brad Bloomfield
3 min readJul 3, 2019

--

Often when interviewing developers I have been asking them to implement a routine to decode 4 bytes in IEEE floating point notation into a floating point number. I provide the algorithm and also some test cases. I then say pick whatever language you like and make it work.

Anyone who did Comp Sci / Engineering / Computational maths might remember that computers only store approximations to floats as machines can only store whole numbers natively. As you might recall this is why float equality is never a good thing to test, unless you’re into creating bugs. Recall learning about things like signs, mantissa and exponents. This is what happens behind the scenes inside CPUs or libraries all for free these days.

I’m pleased to say that everyone that has been given this task has been able to complete it and get it passing all of the tests. So big tick for the most important item, working functional code.

Every person that has done this exercise has done it largely the same way. They convert a String that is 8 characters long (8 hex digits which is actually 4 byes) into a string that is 32 chars long containing only 1s and 0s representing the same 4 bytes. So they effectively use 8 bits to store 1 bit of data. They then use array operations to slice the bits they require out of the 32 bit long array.

After congratulating them on writing working code, I then ask about optimisations. What could be improved here? The usual answers are to do with loops and temporary variable use that kind of thing.

I then start to prompt, something along the lines of, you have 32 bits (4 bytes) of data and if you use a C String this will be 33 bytes. How can I remove the 29 bytes of wasted memory? I often have people think I’m frugal with memory. They think about ways of not using the array but they’re still using a temporary byte to represent a bit.

So then I start probing about the wasted instructions. If you use an array library function in any language it will require many instructions to access a slice of data. It would be fair to assume that there is significant overhead in bounds checking, plus often you need to provide a buffer for the result to be returned into. So it could take anywhere 20 to 100 instructions to perform this kind of function.

I then ask can I extract a certain number of bits in one or two processor instructions? The answer is most often probably because I’m asking this question. Yet nobody has been able to provide an answer as to how they would be able to perform such an operation.

I then make the suggestion of using bitwise operations. The response is usually “what are they?”. Which of course leads me to believe that in our modern software world most developers have not been introduced to such features. Most languages I have used support this. I was surprised to see that even VBA in Excel had support for bitwise operators.

If you’re interested in how these bitwise operators work see the following articles

--

--

Brad Bloomfield
Brad Bloomfield

Written by Brad Bloomfield

technologist, musician, 4WD enthusiast and amateur radio operator and all round geek