Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Bit Stuffing

Name: Anonymous 2011-03-07 23:00

I have three numbers that I'm limiting to 10 bits each and then hoping to packing into a 32-bit standard int.  This would be easy if the numbers were just positive, but they also could be negative.  I end up fighting with the 2s complement.  I know someone is going to suggest just saving myself some headache and forcing the last bit to represent a truncated positive/negative flag, but I really need all ten of those bits for the number (anyone have a 33-bit data type they can spare?).

Now I've already looked online and there isn't much to digest about the matter.  Of the two lonely solutions I've found, one doesn't seem to work and the other is a hassle to implement.  Knowing what I do about 2s complement I'm inclined to believe the latter is the case - it's a hassle no matter what - but I'm also stubborn enough to be believe that there is a more elegant solution.

Name: Anonymous 2011-03-07 23:09

Presuming num_unpacked is a 32-bit int with one of the 10-bit numbers from some num_packed in its lowest 10 bits:
num_unpacked |= (~0 * ((1<<9) & num_unpacked));
i.e., if the highest bit of the 10bit number means it's negative, make the 'unused' bits 1 to make it negative.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List