c - Code explanation -



c - Code explanation -

i don't know if right se site posting, i'll seek either way, answer.it suggested this

if (data[c] >= 128) sum += data[c];

can turn this:

int t = (data[c] - 128) >> 31; sum += ~t & data[c];

can explain me how work?

the thought behind happening this. assume data[c] > 128. if true, doing data[c] - 128 results in positive number (ie. sign bit 0). shifting right 31 times results in number 0s in binary. t=00000000000000000000000000000000.

now, when ~t becomes 1s , &-ing data[c] gives data[c] again. add together sum , works before.

but if data[c] < 128? means data[c] - 128 negative, giving 1 sign bit. means t=11111111111111111111111111111111. ~t 0s. &-ing 0s data[c] gives 0s 1 time again (ie. 0 in decimal). adding 0 sum doesn't alter well.

c

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -