## Bit Play

The first one is to add two integers using bit operators.

int add(int a,int b)

{

if(!a)

return b;

else

return(add((a&b)<<1,a^b));
}[/sourcecode]
The next is to find maximum and minimum of two numbers :

r = y + ((x – y) & -(x < y)); // min(x, y) r = x – ((x – y) & -(x < y)); // max(x, y) where r is the result.

NJOY!!!

-fR0D

Advertisements

could you please throw some light on the utility of bit operators? are they faster than the non-bit operators, since they tweak only few bits instead of the whole bunch of bits? thats my best guess..

SaurabhMarch 29, 2012 at 12:22 PM

Bitwise operations are fast because they translate directly to hardware level instructions. Other operations have to ultimately be converted into them.

fR0DDYMarch 29, 2012 at 12:34 PM