Algorithm-byte high/low bit Switching

Source: Internet
Author: User

Problem
For a single byte of data, the high and low bits are exchanged one by one, for example, 11010001. After the 0-7, 1-6, 2-5, 3-4 bits are exchanged, the value is 10001011.

Solution
To solve this problem, we first think of bitwise processing of the original bytes through the shift operation, and using another variable to store the exchanged results. This solution is clear in mind and it is not difficult to write code.

 

The following code corresponds to this idea:


[Cpp]
Unsigned char shift_fun1 (unsigned char data)
{
Unsigned char I;
Unsigned char tmp = 0x00;
 
For (I = 0; I <8; I ++)
{
Tmp = (data> I) & 0x01) | tmp;
If (I <7)
Tmp = tmp <1;
}
 
Printf ("after shift fun1 data = % x \ n", tmp );
 
Return tmp;

}

The above code is not difficult to implement, and the efficiency is still relatively high. However, there are more concise solutions. In embedded development, we often use the butterfly-type exchange method and table-Store Method to Solve the Problem of byte switching. As the name suggests, The lookup method stores some values in the memory. You only need to look up the table During computation, but it also occupies additional storage space. Here we will introduce the butterfly exchange method.
The so-called butterfly exchange is like this:


[Cpp]
Data = (data <4) | (data> 4 );
Data = (data <2) & 0xcc) | (data> 2) & 0x33 );
Data = (data <1) & 0xaa) | (data> 1) & 0x55 );

We can perform the following calculation:
Assuming that the original order is 0 1 0 1 1 0 0 1

Data = (data <4) | (data> 4); the subsequent sequence is 1 0 0 1 0 1 0 1

Data = (data <2) & 0xcc) | (data> 2) & 0x33); then the sequence is 0 1 1 0 0 1 0 1 1
Data = (data <1) & 0xaa) | (data> 1) & 0x55); then the sequence is 1 0 0 1 1 0 1 0

More abstract, the original bit is 1 2 3 4 5 6 7 8.

Data = (data <4) | (data> 4); the subsequent order is 5 6 7 8 1 2 3 4
Data = (data <2) & 0xcc) | (data> 2) & 0x33); the subsequent order is 7 8 5 6 3 4 1 2

Data = (data <1) & 0xaa) | (data> 1) & 0x55); the subsequent order is 8 7 6 5 4 3 2 1

This completes the reverse conversion of the entire bit. The specific implementation code is as follows:


[Cpp]
Unsigned char shift_fun2 (unsigned char data)
{
Data = (data <4) | (data> 4 );
Data = (data <2) & 0xcc) | (data> 2) & 0x33 );
Data = (data <1) & 0xaa) | (data> 1) & 0x55 );
Printf ("after shift fun2 data = % x \ n", data );
 
Return data;
}

Summary
It is not a common problem to switch the high/low bits of bytes. When this problem occurs, you must carefully analyze it and be familiar with the C language bit operations, this type of problem can be effectively solved.

 

 

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.