News for hackers. Stuff that matters.
Re:Low Level Bit Hacks You Must Know by larsu on Wednesday October 20, 2010 @10:02AM (#1811217)
Bit Hack #6. Turn off the rightmost 1-bit.

Now it finally gets more interesting!!! Bit hacks #1 - #5 were kind of boring to be honest.

Does anybody know a practical use case for that? I have personally never encountered a situation were I needed to manipulate the right most 1-bit.

Otherwise it's a nice introduction to bit hacking.

Re:Low Level Bit Hacks You Must Know by barrkel on Wednesday October 20, 2010 @12:26PM (#1811790)
Let's say you use a bitmask for keeping track of free registers in a compiler. To "allocate" a register, you'll want to clear a bit, to indicate that the register is no longer free.

That's the context I learned this trick in. It's used in the Embarcadero (ex-Borland) compilers (Delphi, C++, etc.).

Re:Low Level Bit Hacks You Must Know by dfox on Wednesday October 20, 2010 @11:07AM (#1811478)
This is mainly useful for testing whenever given number is integral power of 2 (you gen zero in that case). Otherwise it might be useful for some special purpose counters/timers.
Re:Low Level Bit Hacks You Must Know by RiderOfGiraffes on Thursday October 21, 2010 @06:09AM (#1814869)
If you're handed a bit mask of events or flags then it can be used to iterate through the bits that are set, rather than all the bits.

    while flags!=0:
        right_most = flags & (-flags)
        process(right_most)
        flags &= ~right_most
Re:Low Level Bit Hacks You Must Know by sliverstorm on Thursday October 21, 2010 @12:06AM (#1814241)
Perhaps you are using a microcontroller with memory-mapped control registers?