On the bit arithmetic

Source: Internet
Author: User

Bit arithmetic is a kind of old thing passed down from the assembly language generation, only a few 6 operators. At first glance, you will say that it is useless, then you are only half: standing in the current high-level programming point of view, it is not very useful, but in the hardware underlying programming (such as drivers, embedded development), the need for a strong ability to manipulate hardware, at this time, often involved in the operation.

Bitwise operations have 6 operators, each of which are:& (with), | (or), ^ (XOR), ~ (reverse), >> (Shift right), << (shift left). The meaning of this is shown in the following table:

/tr>
operator
& bitwise AND
| bitwise OR
^ bitwise XOR or
~ inverse
<< Left Move
>>

The author is also because of the single-chip computer contact bit operation. However, it is not always possible to use Atmega128 to illuminate the LED's routine as an example. Next, take a few practical examples to help you log in to the gate of the bit operation:

Example 1. Determine the parity of a number: often encountered in judging a number of odd even cases, at this time, the commonly used method is modulo 2 to take the remainder. The efficiency of this method is slightly slower (albeit a constant) and can be replaced by a bitwise operation. The method is to perform and calculate this number with 1. This way, if the result is 1, which is an odd number, otherwise it is an even number. Principle: The 1th bit of the operand is taken with 1, and the first digit is 1, the number is odd, otherwise the number is even.

Example 2. Swap two integers: if these two integers are x and y, then the most common method is to use the TEMP variable tmp, assign X to TMP, assign Y to X, and finally assign TMP to Y. Such a method undoubtedly requires more memory. The following method does not use the annoying temporary variable.

       x ^= y;         ^= x;         ^= y;

Magical, huh? Principle? Just remember that the XOR action is your own. Two times the same XOR operation for an operand, and then returns to the original value.

Example 3. Look at the following simple code:

      If (x = = a) x= b                 ; else x=a;

This code can also be changed using bit arithmetic. Let it become, the amount ... More of a force lattice. It is equivalent to:

      X=a^b^x;

...... In fact, the bit operation is more than this point, including the subnet mask, iostream state, etc. also used in this operation. and its execution speed is certainly faster than other operations (after all, the computer based on 2 binary). So, the card constants of friends, you can try--

On the bit arithmetic

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.