Bitwise operations in java and java operations

Source: Internet
Author: User

Bitwise operations in java and java operations

There were not many bit operations in the previous project. Today, when I click the leetcode on the topic, I used the bit operation, which is the Divide Two Integers in leetcode.

I. java bit operations:

Bitwise expressions are composed of operands and bitwise operators. bitwise operations are performed on binary numbers of integer types. Bitwise operators can be divided into logical operators (including ~ , &, |, And ^), and shift operators (including >>and <and> ).

1) The Left shift operator (<) can move the operator object on the left of the operator to the specified number of digits on the right of the operator (0 at the low position ). Shifts one digit left (without Overflow) by 2.

2) The "Signed" right shift operator (>) moves the operator object on the left of the operator to the right of the specified number of digits on the right of the operator. The "Signed" right shift operator uses the "symbol extension": If the value is positive, 0 is inserted at the high position; if the value is negative, 1 is inserted at the high position. Shifts one digit to the right is equal to dividing by 2.

3) Java also adds an "unsigned" right shift operator (>>>), which uses "Zero extension": both positive and negative values insert 0 at a high position.

4) if char, byte, or short is displaced, they are automatically converted to an int before the shift. Only the five low positions on the right can be used. This prevents us from moving the unrealistic digits in an int number. If a long value is processed, the final result is long. The type required in leetcode is long.

5) in these 6 operators, only ~ The inverse is the single object operator, and the other five are binary operators.

Ii. Actual java bit operations:

The bitwise operation examples we see in other blogs often give a number and then output the operation through syso.

Program: public class PlusRightMoving {public static void main (String [] args) {System. out. println ("5 >>> 1 =" + (5 >> 1) ;}} output result: 5 >>> 1 = 2

However, it should be noted that the assignment operation is different from that in C/C ++.

In C/C ++, move left and right:

div << 1;res << 1;

However, the value assignment operation must be used for the displacement operation in java:

long div = 1;long res = 1;while(div < dived){    div = div << 1;    res = res << 1;}    if(div != dived){    div = div >> 1;    res = res >> 1;}

If it is just like performing operations in C/C ++, it cannot be compiled.

3. Some practical questions about bit operations:

1)Judge parity

As long as it is determined based on whether the lowest digit is 0 or 1, 0 is an even number, and 1 is an odd number. Therefore, you can use if (a & 1) = 0) instead of if (a % 2 = 0) to determine whether a is an even number. The following program will output all the even numbers between 0 and 100.

2)Exchange two numbers

Int c = 1, d = 2;
C ^ = d;
D ^ = c;
C ^ = d;
System. out. println ("c =" + c );
System. out. println ("d =" + d );

3)Transform symbol

 

For example, for-11 and 11, you can change-11 to 11 through the following transformation method.

 

1111 0101 (Binary)-reverse-> 0000 1010 (Binary)-Add 1-> 0000 1011 (Binary)

 

Similarly, we can change 11 to-11.

 

0000 1011 (Binary)-reverse-> 0000 0100 (Binary)-Add 1-> 1111 0101 (Binary)

For other applications, refer to the blog article:

Http://blog.csdn.net/wfzczangpeng/article/details/51819471

There are many application introductions on 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.