Bits Manipulation

Discuss about the puzzles, but without giving the solutions please !!!

Bits Manipulation

Postby admin » Wed Jan 05, 2011 8:32 am

Write a program to swap odd and even bits in an unsigned integer with as few instructions as possible


Last edited by admin on Wed Jan 05, 2011 8:33 am, edited 1 time in total.
Reason: Edit
User avatar
admin
Site Admin
 
Posts: 333
Joined: Thu Dec 23, 2010 1:27 am

Re: Bits Manipulation

Postby Ton24 » Thu Jan 27, 2011 7:31 pm

I don't really get the point..
why does 1234567 generates an output 2222411 ??
more description ?
thank you
User avatar
Ton24
 
Posts: 12
Joined: Tue Jan 25, 2011 8:57 am

Re: Bits Manipulation

Postby David Berthelot » Thu Jan 27, 2011 9:41 pm

It's a bit level operation, for example:
Input:
01 10 11 00 (which is the decimal value 108 expressed in binary)

then you swap the bits at position 0,2,4,6,... with the ones at positions 1,3,5,7,... , so the output should be
10 01 11 00 (which is the decimal value 156 expressed in binary)

Hope it helps.
User avatar
David Berthelot
 
Posts: 43
Joined: Sun Jan 16, 2011 5:06 pm

Re: Bits Manipulation

Postby sridhar » Tue Aug 09, 2011 8:16 pm

Hi,
My submission is verified and I have checked for lot of values also.
but it fails

Are negative integers also considered

thanks
User avatar
sridhar
 
Posts: 9
Joined: Thu Apr 07, 2011 4:59 am

Re: Bits Manipulation

Postby David Berthelot » Wed Aug 10, 2011 10:39 pm

It's pure binary manipulation so the sign is not interpreted, so I don't think any output is negative. Make sure you use long enough words.
User avatar
David Berthelot
 
Posts: 43
Joined: Sun Jan 16, 2011 5:06 pm

Re: Bits Manipulation

Postby fanendra22 » Wed Dec 07, 2011 8:23 pm

Hi ,

I have submitted the code in java language. I have verified the code with given examples. But after submission it is saying that one test has been failed. Please elaborate which test has been failed.

Thanks
User avatar
fanendra22
 
Posts: 1
Joined: Mon Apr 25, 2011 11:13 pm

Re: Bits Manipulation

Postby Alexey Solodovnikov » Thu Jan 05, 2012 5:18 am

fanendra22 wrote:Please elaborate which test has been failed.

Do you use the long type?
User avatar
Alexey Solodovnikov
 
Posts: 32
Joined: Sun Jun 26, 2011 5:54 am


Return to Puzzles

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron