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

Absolute value without multiplies

Name: FrozenVoid !!mJCwdV5J0Xy2A21 2011-11-04 23:05

http://codepad.org/wByY34pA
//its an improvement on my old bithack of x* ((((x >= 0) | 0) << 1) - 1)
void main(){//abs without multiply
int a=-23;// if neg   +2a -a=a if pos -a + 2a=a
int b= ((-a)<<(a<0))+((a)<<(a>0));   
printf("a:%d b:%d",a,b);
}

Name: FrozenVoid !!mJCwdV5J0Xy2A21 2011-11-04 23:44

*slight typo: ((a)*((a>0)-(a<0)));

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