Re:Low Level Bit Hacks You Absolutely Must Know
by tptacek on Tuesday June 30, 2009 @04:37PM
(#681535)
When, given a bit-vector representation of 8 devices, would I want to do something with the "right-most" of those devices?
Re:Low Level Bit Hacks You Absolutely Must Know
by pkrumins on Tuesday June 30, 2009 @04:42PM
(#681541)
for example, those 8 devices were 8 LEDs, some of them on, and you'd want to turn the rightmost led off. :)
edit: i'll look into more serious applications when i write that article on bit trick applications.
To answer your question, why you'd want to set/unset the right-most 1-bit, suppose you have a space efficient 8 bit data structure that represents 8 devices. Each bit represents if a device is on and off. And you want to turn off the right-most device. Then you could use that hack. Or for example, you want to linearly turn on devices from 1st to 8th, then you can just turn on the rightmost bit eight times, and turn off in the same manner. Or you could have some crazy device that puts data at some memory location and waits you to clear the rightmost bit before it puts new data at that location. Or you have a number system coded in your byte in such a way that rightmost low order bit is always the sign (or some other craziness).