Friday, June 8, 2007

Re: Bit Hacks

Previously i had post on the Bit hacks.. I got those from a site and was quite interested in it. I want people to know that, so i posted a couple of things. One of my senior santhosh popularly known as santy who read my blog asked me about the one which i posted.. It was to find the absolute value of an integer. I worked it out, but could not find it out. can anyone go through it and work it out for me it will be great

note: The CHAR_BIT is normally take as 8

2 comments:

Vijesh said...

(x<0)?-(x):x

Hope this expression answers santy's question!

Tom Praison said...

Some CPUs don't have an integer absolute value instruction (or the compiler fails to use them). On machines where branching is expensive, the above expression can be faster than the obvious approach, r = (v < 0) ? -v : v, even though the number of operations is the same.