wu :: forums
« wu :: forums - REVERSE  bits in 8 byte »

Welcome, Guest. Please Login or Register.
Nov 28th, 2024, 12:35pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: william wu, Icarus, SMQ, Grimbal, ThudnBlunder, Eigenray, towr)
   REVERSE  bits in 8 byte
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: REVERSE  bits in 8 byte  (Read 6724 times)
puzzlecracker
Senior Riddler
****



Men have become the tools of their tools

   


Gender: male
Posts: 319
REVERSE  bits in 8 byte  
« on: Dec 17th, 2004, 11:33pm »
Quote Quote Modify Modify

Using C, but without loops or lookup tables, write a routine which reverses  the bits in an 8-bit byte
 
 
 
IP Logged

While we are postponing, life speeds by
John_Gaughan
Uberpuzzler
*****



Behold, the power of cheese!

5187759 5187759   john23874   SnowmanJTG
WWW Email

Gender: male
Posts: 767
Re: REVERSE  bits in 8 byte  
« Reply #1 on: Dec 18th, 2004, 10:48am »
Quote Quote Modify Modify

This should do the trick. Can anyone make it shorter?
::
unsigned char reverse (unsigned char n)
{
  return ((n & 0x01) << 7)
  | ((n & 0x02) << 5)
  | ((n & 0x04) << 3)
  | ((n & 0x08) << 1)
  | ((n & 0x10) >> 1)
  | ((n & 0x20) >> 3)
  | ((n & 0x40) >> 5)
  | ((n & 0x80) >> 7);
}
::
IP Logged

x = (0x2B | ~0x2B)
x == the_question
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: REVERSE  bits in 8 byte  
« Reply #2 on: Dec 18th, 2004, 2:07pm »
Quote Quote Modify Modify

something like
::
n = n&11110000 >>4 | n&00001111 << 4;
n = n&11001100 >>2 | n&00110011 << 2;
return  n&10101010 >>1 | n&01010101 << 1;
 
I didn't actually bothered to check whether this does what I think it ought to do, but if it doesn't it should give you the idea of what does..
::
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
puzzlecracker
Senior Riddler
****



Men have become the tools of their tools

   


Gender: male
Posts: 319
Re: REVERSE  bits in 8 byte  
« Reply #3 on: Dec 19th, 2004, 1:39pm »
Quote Quote Modify Modify

can both of you, or anyone else pretty much, explain that... thx  
 
 
 I thought to actually flipping the bits as  
 n=n^255, but only flips them....
 
 
I was wrong... please explain the logic.. thx
 
IP Logged

While we are postponing, life speeds by
puzzlecracker
Senior Riddler
****



Men have become the tools of their tools

   


Gender: male
Posts: 319
Re: REVERSE  bits in 8 byte  
« Reply #4 on: Dec 19th, 2004, 3:23pm »
Quote Quote Modify Modify

never mind (i am an idiot) -- towr - you're a genous of modern creation!
IP Logged

While we are postponing, life speeds by
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