Code Zone
Labels
BareMinimum
Done
FrequentlyAsked
RecentQuestions
Revise
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:
Sud
January 2, 2012 at 5:42 PM
int v;
unsigned int r;
int const mask = v >> sizeof(int) * CHAR_BIT - 1;
r = (v + mask) ^ mask;
Reply
Delete
Replies
Reply
Add comment
Load more...
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
int v;
ReplyDeleteunsigned int r;
int const mask = v >> sizeof(int) * CHAR_BIT - 1;
r = (v + mask) ^ mask;