Monday, January 2, 2012

Integer absolute value without branching

Compute the integer absolute value (abs) without branching

Useful Links:

http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs

1 comment:

  1. int v;
    unsigned int r;
    int const mask = v >> sizeof(int) * CHAR_BIT - 1;

    r = (v + mask) ^ mask;

    ReplyDelete