Invert a byte using bitwise operations

Source: Internet
Author: User

I still don't like bitwise operations... Ah...

I just saw a post about a netizen's bitwise operation reversing one byte on the Internet and posted it to learn to accumulate...Code:

 
UnsignedCharReverse8 (unsignedCharC)
{
C = (C &0x55) <1| (C &0xaa)>1;
C = (C &0x33) <2| (C &0xcc)>2;
C = (C &0x0f) <4| (C &0xf0)>4;
ReturnC;
}

Analysis:

ItsAlgorithmYes:The first is a group of two digits and two digits. the first half and the last half are exchanged. The first half and the last half are exchanged. The first half and the last half are exchanged.For example,
Reverse 1 2 3 4 5 6 7 8.
(1) Two and two are in a group, and the first half and the last half of the exchange are changed to: 2 1 4 3 6 5 8 7
(2) four and four are in one group, and the first half and the last half of the exchange are changed to: 4 3 2 1 8 7 6 5
(3) The first half and the last half of the exchange are 8, 7, 6, 5, 4, 3, and 1.
Reversed successfully.
Such an algorithm is simple, and it is easy to use mathematical induction to prove its correctness. This function cleverly implements parallel computing and grouping, and it completes computing at one time.

First read the first statement. C = (C & 0x55) <1 | (C & 0xaa)> 1;
0x55 is actually 01010101, 0xaa is 10101010
Suppose c = abcdefgh
C & 0x55 = 0b0d0f0h, C & 0xaa = a0c0e0g0
Follow, the former shifts one digit to the left, b0d0f0h0, the latter shifts one digit to the right, 0a0c0e0g, and another | operation, and the two digits exchange positions.
Imagine that you have a long piece of paper, which is divided into one cell and written one word per cell. If you cut a small hole every cell, slide one cell, and cover the original paper, then you can see that two words are exchanged.
(Note: | operations can be changed to + operations. Why? Actually, it's really easy. The result of adding a number to 0 is still the number... is that true ?)

The second statement. C = (C & 0x33) <2 | (C & 0xcc)> 2;
0x33 = 00110011, 0xcc = 11001100.

The third statement. C = (C & 0x0f) <4 | (C & 0xf0)> 4;
0x0f = 00001111, 0xf0 = 11110000.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.