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: Anonymous 2011-11-05 5:24

>>15
That'd be a branch.

Name: FrozenVoid !!mJCwdV5J0Xy2A21 2011-11-05 5:41

>>15
First bit does not ==sign, its odd/even, you mean most-significant bit(x>>31 for Int32) you still have to convert that bit into sign(0 or -1 to 1:-1), and its limited to ints with 4bytes length.
http://codepad.org/Q7yDw1U8 you probably mean something like this

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