0

This is more a follow up to https://stackoverflow.com/a/5587983/13586005. @sam hocevar or anybody else who understands this: Would you mind explaining what is happening here:

tmp = (tmp - 0x70) & ((unsigned int)((int)(0x70 - tmp) >> 4) >> 27);

I'm not sure I fully follow it. I understand that (tmp - 0x70) is correcting for the 127->15 bias, but I don't understand the 2nd part((unsigned int)((int)(0x70 - tmp) >> 4) >> 27) and therefore don't understand the & with the corrected bias in the last step. Thanks!

ptpaterson
  • 8,578
  • 4
  • 25
  • 38

1 Answers1

0
(unsigned int)((int)(0x70 - tmp) >> 4) >> 27

is equivalent1 to

(int)(0x70 - tmp) < 0 ? 0x1f : 0

but guarenteed to not involve a branch -- it instead extracts the sign bit from the result of the extraction, replicates it 4 times, then downshifts to get either 0x1f or 0 depending.


1Under the assumption that signed right shifts of negative numbers do a proper arithmetic shift -- not guarenteed by the C spec, but common on most implementations

Chris Dodd
  • 111,130
  • 12
  • 123
  • 212