C++ Learning Community Forum
August 01, 2010, 02:57:59 AM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News
: Hello.
Home
Help
Search
Login
Register
C++ Learning Community Forum
>
General C++ Programming
>
C++ Tips
>
Reverse Bits
Pages: [
1
]
« previous
next »
Print
Author
Topic: Reverse Bits (Read 1005 times)
myork
Global Moderator
C++ guru
Posts: 1147
Reverse Bits
«
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
Re: Reverse Bits
«
Reply #1 on:
October 08, 2008, 12:38:22 AM »
I know exactly which question on SO you saw this in.
(I like how you're plugging this forum on there as well BTW!
)
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
Re: Reverse Bits
«
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
Re: Reverse Bits
«
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
Re: Reverse Bits
«
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
« previous
next »
Jump to:
Please select a destination:
-----------------------------
General C++ Programming
-----------------------------
=> C++ Help
===> C Programming
=> C++ Tips
=> C++ in General
=> 3rd Party Libraries Programming
-----------------------------
Platform Specific Programming
-----------------------------
=> Windows
=> Linux
=> DirectX
=> OpenGL
-----------------------------
C++ Creations
-----------------------------
=> Finished Products
=> Work in Progress
-----------------------------
Resources
-----------------------------
=> Recommended Resources
=> Bookworm's Corner
===> Accelerated C++: Practical Programming by Example
===> Code Complete
-----------------------------
Other than C++....
-----------------------------
=> Off Topic
=> Other Programing Languages
===> ASM Programming
=> Computer Lab
=> The Community
Loading...