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 9:04

Fastest way to get signed that I can imagine:

Bitwise AND along with the largest possible number for that data type

e.g. signed char to unsigned value:

Maximum signed char value:

0111 1111 (127)

Arbitrary signed char value:

1010 0111 (-39)

Bitwise &:

1010 0111 & 0111 1111

0010 0111

No idea why you rape your mother though.

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