A blog about programming and more programming.

Bit Play

with 2 comments

The first one is to add two integers using bit operators.
int add(int a,int b)
return b;
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.



Written by fR0DDY

February 23, 2009 at 5:13 AM

Posted in Beautiful Codes

Tagged with ,

2 Responses

Subscribe to comments with RSS.

  1. 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..


    March 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.


      March 29, 2012 at 12:34 PM

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: