Low Level Bit Hacks You Must Know
catonmat.net
Low Level Bit Hacks You Must Know
1–10 of 49 posts
Re: Low Level Bit Hacks You Must Know
#2Now 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
#3Re: Low Level Bit Hacks You Must Know
#4Here's a much more comprehensive collection of bit hacks: http://graphics.stanford.edu/~seander/bithacks.html
Re: Low Level Bit Hacks You Must Know
#5Re: Low Level Bit Hacks You Must Know
#6Re: Low Level Bit Hacks You Must Know
#7[1] http://www.amazon.com/Hackers-Delight-Henry-S-Warren/dp/0201...
Re: Low Level Bit Hacks You Must Know
#8Bit 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
#9 bitvec read_nbits (unsigned int count, bitstream input);
bool write_nbits (unsigned int count,bitvec bits,
bitstream output);
Then add a multirecord I/O. That is, read a record of bitvecs, each N bits wide, where the length is not given but encoded in the bitstream in this manner: read a bitvec record of N bits, if the high-bit is set (take it in whatever endian you like), read another record and repeat, if the high bit is not set, return whatever you read thus far.Then add the ability to return the bitvec records as single integers (bignums even) both signed and unsigned, taking them in this manner. If the bitvecs are unsigned, each record contributes its 7 least significant bits, and the high bit is dropped. Taking the bit on either end of the first record as the most signficant, iterate over the rest and shift and OR according to your endian needs to construct an integer from the sum of all the bits.
When I first did this exercise, I was very close to gouging my own eye-balls out, but after I did it, I started to think of bits as something very natural, and not to be feared.