C++ Learning Community Forum
August 01, 2010, 02:57:59 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Reverse Bits  (Read 1005 times)
myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« on: October 07, 2008, 07:19:54 PM »


Reverse the bits in an integer

Code:
unsigned int reverse(register unsigned int x)
{
 x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
 x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
 x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
 x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
 return((x >> 16) | (x << 16));
}
Logged
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #1 on: October 08, 2008, 12:38:22 AM »

I know exactly which question on SO you saw this in. Grin (I like how you're plugging this forum on there as well BTW! Smiley)

Don't that code kinda assume the unsigned int is a certain size??
Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
syazhani
I wonder how long can titles be... because the longer it is the more attention I can get!
Dr. of C++ology
****
Posts: 529


Cats > Dogs


View Profile
« Reply #2 on: October 08, 2008, 05:15:40 AM »

Saw this one too. The analogliterals one is interesting.
Logged

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."- Brian W. Kernighan

What people misunderstood about Islam presented in comical way: http://ummahfilms.com
oulyt
C++ Freak
***
Posts: 340



View Profile
« Reply #3 on: October 11, 2008, 03:24:50 PM »

im so lost. i went to that site syazhani. and i tried out the stuff on it and i threw it into my compiler and i got nothing but lines and lines and lines of errors
Logged
oldneuro
N00b!!1
*
Posts: 17


View Profile
« Reply #4 on: November 04, 2008, 05:14:27 AM »

this code is lifted directly from CSCI2021 at the U of M (and probably many other machine architecture classes), and is a common assignment problem.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!