Reverse a number

Keep Thinking

Given a unsigned int number, how to reverse it?

The $latex O(n)$ method is easy, we hope to do it faster.
Swap the odd and even position, and then swap every two-length position and its next, and so on…
Example:
10010010
(1 0 0 1 0 0 1 0) -> (0 1 1 0 0 0 0 1)
(01 10 00 01) -> (10 01 01 00)
(1001 0100) -> (0100 1001)
01001001
Here goes the $latex O(\log n)$ way:

View original post

Advertisements
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s