Of course we can create a template to get absolute value from any type of integers:
Code:
#define BYTE_SIZE 8
template <typename T>
T mabs(T x)
{
T t = x >> (sizeof(T)*BYTE_SIZE - 1);
return t ^ (x + t);
}
usage
Code:
long int z = mabs<long int>(-11547);
Now, it looks funny since it seems as if this can be used to get "absolute" value of any structure or class. It may but only if >> is defined. For example >> is not defined for
double and
float.