Full summary of Java bit operations

Source: Internet
Author: User

All the data in the computer is stored in binary form. Bit arithmetic is the direct manipulation of binary data in memory, so processing data is very fast. In the actual programming, if can skillfully use bit operation, can achieve 42 to dial the effect, just because bit operation of these advantages, so bit operation in the major IT company's written interview has been a hot issue.

Bit Operation Basics

The basic bit operations are 6 kinds of have, or, XOR, inverse, left, and right, and their operation rules are as follows:

Note the following points:

    1. In these 6 kinds of operators, only the ~ is the monocular operator, the other 5 are binocular operators.
    2. Bit operations can only be used for shaping data, and bitwise operations on float and double types will be error-checked by the compiler.
    3. Bitwise operators have a lower priority because they use parentheses to ensure the order of operations, otherwise they are likely to get the results. For example, to get numbers like 1,3,5,9 these 2^i+1. write int a = 1? i + 1; no, the program executes i + 1 First, then the left shift operation. should be written as int a = (1? i) + 1;
    4. There are also some composite operators in other bits, such as &=, |=, ^=,? =,? =.
package com.king.bit;/** * @author taomk * @version 1.0 * @since 15-5-10 下午2:23 */public class BitMain {    public static void main(String [] args) {        int a = -15, b = 15;        System.out.println(a >> 2); // -4:-15 = 1111 0001(二进制),右移二位,最高位由符号位填充将得到1111 1100即-4        System.out.println(b >> 2); // 3:15=0000 1111(二进制),右移二位,最高位由符号位填充将得到0000 0011即3    }}
Common bit Operation tips

Here are some common applications of bit operations to make a summary, there are judgment parity, exchange two number, transform symbols and seek absolute value. These tips are easy to remember and should be mastered.

Judging odd and even

As long as the least bit is 0 or one decision, 0 is an even number, 1 is an odd number. You can therefore use if (A & 1) = = 0) instead of if (a% 2 = = 0) To determine if a is an even number. The following program outputs all the even numbers between 0 and 100:

    for (int i = 0; i < 100; i ++) {        if ((i & 1) == 0) { // 偶数            System.out.println(i);        }    }
Exchange two number
int c = 1, d = 2;c ^= d;d ^= c;c ^= d;System.out.println("c=" + c);System.out.println("d=" + d);

This can be understood as:
The first step a=b namely a= (AB);
The second step b=a namely B=b (AB), because the operation satisfies the commutative law, B (AB) =bba. Because a number and its own XOR result is 0 and any number with 0 XOR will not change, so at this time B is assigned a value;
The third step a=b is A=ab, because the previous two steps to know A= (AB), b=a, so A=ab is a= (AB) A. So a will be assigned the value of B;

Transform symbols

A transform symbol is a positive number and a negative number becomes a positive number.
For 11 and 11, you can change-11 to 11 by the following transformation method

1111 0101 (binary) – Reverse, 0000 1010 (binary) – Add 1-> 0000 1011 (binary)

It's also possible to turn 11 into a-11

0000 1011 (binary) – Reverse, 0000 0100 (binary) – Add 1-> 1111 0101 (binary)

So the transform symbol only needs to be reversed and added 1. The complete code is as follows:

int a = -15, b = 15;System.out.println(~a + 1);System.out.println(~b + 1);

Full summary of Java bit operations

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.