Author |
Topic: REVERSE bits in 8 byte (Read 6724 times) |
|
puzzlecracker
Senior Riddler
Men have become the tools of their tools
Gender:
Posts: 319
|
|
REVERSE bits in 8 byte
« on: Dec 17th, 2004, 11:33pm » |
Quote 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!
Gender:
Posts: 767
|
|
Re: REVERSE bits in 8 byte
« Reply #1 on: Dec 18th, 2004, 10:48am » |
Quote 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:
Posts: 13730
|
|
Re: REVERSE bits in 8 byte
« Reply #2 on: Dec 18th, 2004, 2:07pm » |
Quote 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:
Posts: 319
|
|
Re: REVERSE bits in 8 byte
« Reply #3 on: Dec 19th, 2004, 1:39pm » |
Quote 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:
Posts: 319
|
|
Re: REVERSE bits in 8 byte
« Reply #4 on: Dec 19th, 2004, 3:23pm » |
Quote Modify
|
never mind (i am an idiot) -- towr - you're a genous of modern creation!
|
|
IP Logged |
While we are postponing, life speeds by
|
|
|
|