---
title: "Absolute Value Function"
type: concept
source: https://www.jemoka.com/posts/kbhabsolute_value_function/
confidence: high
status: active
---

Here&rsquo;s a fun implementation of absolute value.
long abs_value(long num) { long sign = num &gt;&gt; (sizeof(long)*CHAR_BIT - 1); // so you only get all 1s or all 0s return (num ^ sign) - sign; // sign is either -1 or 0. So, if num is non-negative // num^sign is not going to do anything (as 0^0 = 0, 0^1 = 1). // If num negative, num^sign is going to flip the bit AND subtract // negative on (i.e. add one) }
