Joined: 23 Nov 2006 Posts: 19270 Location: Inside the Matrix
Hello and welcome to the forums,
Is this bit manipulation internal to the code or is there data in one or more files that contain bit settings? If the bits are only used internally, i'd look at converting them to bytes instead - much easier to work with. If there are bit settings in your data, you can expand and compress it by working on one byte at a time and using powers of 2 to construct 8 on/off switches.
Keep in mind that all of the values from zero to 255 can be represented in 1 byte. If you want to de-compose a byte, move it to the low-order byte of a 2-byte comp field. If the the value is >127, the high-order bit is marked "on". Subtract 127 and see if the remaining value is >63. If it is, the next bit is marked "on" and so forth.
If you want to build a byte from individual settings, use the same 8 on/off switches and create the "total" by adding 128, 64, 32 etc for the corresponding "on" bits. Use the same 2-byte comp field - make sure you zero it before starting to accumulate.
There may be a more elegant way to do this, but this method has worked on many compilers for many years.