JAVA basics/Lesson 14th: operators/All JAVA operators 3. java Operators

Source: Internet
Author: User

JAVA basics/Lesson 14th: operators/All JAVA operators 3. java Operators

I. JAVA bit Operators

Bitwise operators are not common in actual work, but they are always very fond of these bitwise operations.
Therefore, this section provides operation examples for each operator to help you understand its specific meaning.
Finally, if you are interested, you can skip this chapter. Let's take a look.
 

1. Binary Expression of an integer

Bitwise operations are binary, but we usually use decimal, for example, 5. The binary value of 5 is 101.
Before learning, you need to know the Binary Expression of an integer.
Use the Integer. toBinaryString () method to convert a decimal Integer into a binary string.

Public class HelloWorld {public static void main (String [] args) {int I = 5; String B = (Integer. toBinaryString (I); // The Binary Expression of 5 101 System. out. println (the Binary Expression of I + "is:" + B );}}
2. Bit or |

The binary value of 5 is 101; the binary value of 6 is 110.
Therefore, 5 | 6 performs or operations on each bit to obtain 111-> 7.

Public class HelloWorld {public static void main (String [] args) {int I = 5; int j = 6; System. out. println (Integer. toBinaryString (I); // The binary number of 5 is 101 System. out. println (Integer. toBinaryString (j); // The binary of 6 is 110 System. out. println (I | j); // so 5 | 6 performs the or operation on each bit, and the result is 111-> 7 }}
3. bitwise AND &

The binary value of 5 is 101; the binary value of 6 is 110.
Therefore, perform operations on each bitwise in the 5 & 6 pairs and obtain the value 100-> 4.

Public class HelloWorld {public static void main (String [] args) {int I = 5; int j = 6; System. out. println (Integer. toBinaryString (I); // The binary number of 5 is 101 System. out. println (Integer. toBinaryString (j); // The binary of 6 is 110 System. out. println (I & j); // Therefore, perform operations on each digit in the 5 & 6 pairs and obtain the result 100-> 4 }}
4. Exclusive or ^

The binary value of 5 is 101; the binary value of 6 is 110.
So we perform an exclusive or operation on each bit on the 5 ^ 6, and get the result from 011-> 3.
Some special situations:
Any number is equal to or equal to 0.
The value of any number is equal to or equal to the value of 0.

Public class HelloWorld {public static void main (String [] args) {int I = 5; int j = 6; System. out. println (Integer. toBinaryString (I); // The binary number of 5 is 101 System. out. println (Integer. toBinaryString (j); // The binary of 6 is 110 System. out. println (I ^ j); // Therefore, the bitwise OR operation is performed on each digit in the 5 ^ 6 column to obtain the 011-> 3 System. out. println (I ^ 0); System. out. println (I ^ I );}}
5. Retrieve non ~

The binary value of 5 is 00000101.
So the inverse value is 11111010.
Convert the binary value to-6 in decimal format.

Public class HelloWorld {public static void main (String [] args) {byte I = 5; System. out. println (Integer. toBinaryString (I); // The binary value of 5 is 00000101. Therefore, if the value is not 11111010, it is-6 System. out. println (~ I );}}
6. Shift left and right

Left shift: Based on the Binary Expression of an integer, move each bit to the left, and add 0 to the rightmost bit.
Right Shift: Based on the Binary Expression of an integer, move each bit to the right.

Public class HelloWorld {public static void main (String [] args) {byte I = 6; // The binary value of 6 is 110 System. out. println (Integer. toBinaryString (I); // 6 shifts the value of 1 to the left and changes to 1100. The corresponding 10 hexadecimal value is 12 System. out. println (I <1); // 6 shifts the value of 1 to the right and changes to 11. The corresponding 10-digit System is 3. out. println (I> 1 );}}
7. Shift right with or without symbols to the right

Shift to the right with a symbol>
For positive numbers, shifted to the right with a symbol> shifts all the bits to the right and adds 0 at the beginning.
For negative numbers, shifted to the right with a symbol> shifts all the bits right and Adds 1 at the beginning.
Unsigned shift to the right >>>
If it is a negative number, the first digit of the corresponding binary is 1.
Unsigned shift to the right >>> the first 1 is also moved to the right. As a result, after moving, the first 1 is changed to 0.
In this way, a positive number is obtained after the unsigned right shift of the negative number.
To put it simply:
Shift to the right with a symbol> move the back positive or positive, negative or negative, and the symbol remains unchanged
Unsigned shift to the right> after moving, it becomes positive

Public class HelloWorld {public static void main (String [] args) {int I =-10; // The binary value of-10 is 11111111111111111111111111110110 // the first value is 1, that is, the sign bit, indicates that this is a negative number System. out. println (Integer. toBinaryString (I); // for positive numbers, shifted to the right with a symbol> This shifts all bits right and adds 0 at the beginning. // for negative numbers, shifted to the right> all the bits are shifted to the right, and the first part is filled with 1 //-10 to the right, after moving, fill in 1 // get 11111111111111111111111111111011 // because the first digit is 1, it is still a negative number, and the corresponding decimal is-5 int j = I> 1; System. out. println (Integer. toBinaryString (j); System. out. println (j); //-10 unsigned shifts one digit to the right, and the symbol bit is also shifted to the right. The first digit is changed to 0 // get 01111111111111111111111111111011, the corresponding decimal value is 2147483643 int k = I >>> 1; System. out. println (Integer. toBinaryString (k); System. out. println (k );}}

 

Related Article

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.