ABS() function implementation
Hi,
How can I write a code for ABS() function, without any condtion check? Usually ABSOLUTE function is implemented as int abs(int var) { if ( var < 0) var = var; return var; } I do not want to have any conditional statement. Thanks Sandeep 
Re: ABS() function implementation
Type cast it to unsigned int and then return the value

Re: ABS() function implementation
You can disguise but you can't easily remove if statement since the extraction from an integer is different if the number is positive or negative. This is one of the solutions that basically says: if number is negative do a bitinversion after subtracting 1 from it, otherwise do nothing.
Code:
int my_abs(int x) Code:
int my_abs(int x) 
Re: ABS() function implementation
Of course we can create a template to get absolute value from any type of integers:
Code:
#define BYTE_SIZE 8 Code:
long int z = mabs<long int>(11547); 
All times are GMT +5.5. The time now is 16:00. 