wu :: forums
« wu :: forums - Integer Reversal »

Welcome, Guest. Please Login or Register.
Dec 1st, 2024, 4:44am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Icarus, towr, Eigenray, Grimbal, SMQ, william wu, ThudnBlunder)
   Integer Reversal
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Integer Reversal  (Read 1650 times)
ThudnBlunder
wu::riddles Moderator
Uberpuzzler
*****




The dewdrop slides into the shining Sea

   


Gender: male
Posts: 4489
Integer Reversal  
« on: May 22nd, 2004, 9:01am »
Quote Quote Modify Modify

What is the shortest C function that takes an int as argument and returns an int with all the bits of the argument reversed? That is, the jth bit of the result is the (n-j)th bit of the argument, where n is the width of an int in bits.
(The function must work for arbitrary widths and not make any assumptions about the size of int or char.)  
 
« Last Edit: May 22nd, 2004, 9:13am by ThudnBlunder » IP Logged

THE MEEK SHALL INHERIT THE EARTH.....................................................................er, if that's all right with the rest of you.
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Integer Reversal  
« Reply #1 on: May 22nd, 2004, 9:54am »
Quote Quote Modify Modify

"no assumptions" means that the source should work on a hypothetical computer with 777-bit integers?
 
"shortest", does it mean in source size or in execution speed?
IP Logged
ThudnBlunder
wu::riddles Moderator
Uberpuzzler
*****




The dewdrop slides into the shining Sea

   


Gender: male
Posts: 4489
Re: Integer Reversal  
« Reply #2 on: May 22nd, 2004, 10:17am »
Quote Quote Modify Modify

Quote:
"shortest", does it mean in source size or in execution speed?

Source size, the number of its characters.
IP Logged

THE MEEK SHALL INHERIT THE EARTH.....................................................................er, if that's all right with the rest of you.
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Integer Reversal  
« Reply #3 on: May 22nd, 2004, 3:15pm »
Quote Quote Modify Modify

unsigned rev1(unsigned n){
    unsigned m;
    for(m=n%2,n=~(~n/2);n-1;n/=2)m+=m+n%2;
    return m;
}
 
PS: unfortunately, it won't work with int's
« Last Edit: May 22nd, 2004, 3:20pm by Grimbal » IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Integer Reversal  
« Reply #4 on: May 23rd, 2004, 9:29am »
Quote Quote Modify Modify

ten more characters, but it works with int Tongue
 
int rev(int n)
{
  unsigned r=0, m=(unsigned)n, c=1;
  for(;c>r;c*=2){r=r*2+m%2;m/=2;}
  return (int)r;
}
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Integer Reversal  
« Reply #5 on: May 23rd, 2004, 10:56am »
Quote Quote Modify Modify

Still can be shortened.
 
rev3(n){
  int r=0,c=1;
  for(;c;c*=2)r+=r+(n&1),n>>=1;
  return r;
}
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Integer Reversal  
« Reply #6 on: May 23rd, 2004, 11:04am »
Quote Quote Modify Modify

why not
r+=r+n%2
instead of?
r+=r+(n&1)
 
And what about the types? Or are we ignoring compiler warnings?
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
ThudnBlunder
wu::riddles Moderator
Uberpuzzler
*****




The dewdrop slides into the shining Sea

   


Gender: male
Posts: 4489
Re: Integer Reversal  
« Reply #7 on: May 23rd, 2004, 11:33am »
Quote Quote Modify Modify

How about  
:
b(n){return n?b(n<<1)<<1|n<0:0;}  
IP Logged

THE MEEK SHALL INHERIT THE EARTH.....................................................................er, if that's all right with the rest of you.
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Integer Reversal  
« Reply #8 on: May 23rd, 2004, 11:45am »
Quote Quote Modify Modify

I can't compile it..
otherwise I'm sure it's fine Tongue
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Integer Reversal  
« Reply #9 on: May 23rd, 2004, 11:54am »
Quote Quote Modify Modify

towr: n%2 doesn't give 0 or 1 with negative numbers
 
With gcc, I get no warning.  I probably depends on your compiler.
 
THUDandBLUNDER: nice code!  You still can replace <<1 by *2
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Integer Reversal  
« Reply #10 on: May 23rd, 2004, 12:06pm »
Quote Quote Modify Modify

on May 23rd, 2004, 11:54am, grimbal wrote:
With gcc, I get no warning.  I probably depends on your compiler.
Must depend on the version then as well, as I also used gcc..
« Last Edit: May 23rd, 2004, 12:24pm by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
ThudnBlunder
wu::riddles Moderator
Uberpuzzler
*****




The dewdrop slides into the shining Sea

   


Gender: male
Posts: 4489
Re: Integer Reversal  
« Reply #11 on: May 23rd, 2004, 12:17pm »
Quote Quote Modify Modify

I got the problem here.
IP Logged

THE MEEK SHALL INHERIT THE EARTH.....................................................................er, if that's all right with the rest of you.
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Integer Reversal  
« Reply #12 on: May 23rd, 2004, 1:05pm »
Quote Quote Modify Modify

Aha.  They even thought of the *2, but consider it is not ANSI conforming, because there would be an overflow to the sign bit.
IP Logged
ThudnBlunder
wu::riddles Moderator
Uberpuzzler
*****




The dewdrop slides into the shining Sea

   


Gender: male
Posts: 4489
Re: Integer Reversal  
« Reply #13 on: May 23rd, 2004, 1:15pm »
Quote Quote Modify Modify

on May 23rd, 2004, 1:05pm, grimbal wrote:
Aha.  They even thought of the *2, but consider it is not ANSI conforming, because there would be an overflow to the sign bit.

I would have pointed that out but I wouldn't have known what I was talking about as I don't know C.    
(And I always know what I am talking about.)   Roll Eyes
 
« Last Edit: May 26th, 2004, 8:14am by ThudnBlunder » IP Logged

THE MEEK SHALL INHERIT THE EARTH.....................................................................er, if that's all right with the rest of you.
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board