3 ms·
One thing I had to do years ago as part of an interview was to write a C function to find the first bit set in a block of memory, flip it and return the locatio
by arethuza 16y ago
One thing I had to do years ago as part of an interview was to write a C function to find the first bit set in a block of memory, flip it and return the location of the bit. Doing that at a whiteboard while explaining to the bearded chap interviewing me what I was doing.
From what I recall, I actually sort of enjoyed solving it as my C was bit rusty as I'd mostly been programming in Lisp.
- barrkel 16y agoA neat trick for clearing the lowest bit is subtracting one and and'ing that with the original: x_cleared = x & (x - 1); You can get the lowest bit from the difference.
- bnoordhuis 16y agoIsn't this easier and faster, both type- and performance-wise? x_cleared = x & ~1;
- jonsen 16y agoThe intension is to clear the rightmost 1-bit.
- arethuza 16y agoI'm pretty sure that doesn't do what I was asked to do.