Introduction to Java Basics essay (3) version javase

Source: Internet
Author: User
Tags bitwise bitwise operators

The previous section notes some of the operators ' considerations, and the beginning of this section is still an elaboration of some of the operators ' points of attention!

Comparison operators need to be aware of instanceof, except for >, >=, <, <=, = =,! =, to check if the object is a class, for example: "Hello" instanceof String, which returns the result bit true.

1. Logical operators

& (and) operation characteristics: The operation of the two sides as long as there is a false, the result is definitely false, only the two sides are true, the result is true;

| (or) operation characteristics: As long as there is a true on both sides of the operation, the result is definitely true, only both sides are false, the result is false;

^ (XOR) Operation characteristics: The results of both sides of the operation if the same, the result is false, the results are different on both sides, the result is true;

! (non) Operational characteristics: Judging the other side of things;

&& (short-circuit double and) operation characteristics: and the & operation is basically the same, just && when the left is false, the right does not participate in the operation, and & both sides have to operate;

|| (short-circuit dual or) of the operation characteristics: and | Operation basically consistent, just | | When the left is true, the right side does not participate in the operation, but | Both sides have to be calculated;

2. Bitwise operators

& (with bitwise operators): The operations feature is the same as the & of the logical operators, except for binary bitwise operations, such as:6&3=2; (binary principle: 110&011=010), any number with a bit of 1 fetch & The operation is the original number (the function takes some significant number of digits);

| (or operator): the & of the arithmetic characteristic or logical operator is just the bit operation for the binary, for example: 6|3=7; (binary principle: 110|011=111);

^ (XOR operator): The arithmetic characteristic is the same as the ^ of the logical operator, but here is the bitwise operation for the binary, for example: 6^3^3 = 6, note: When a number is different or the operation is the same number two times, the result is the number itself (the effect of the encryption)

~ (Inverse code operator): The operation characteristics are the same as in the operator's logical operators!

<< (left shift operator): The number of left shifts is actually the number of times the data is multiplied by 2. You can complete the power operation of 2!

>> (right shift operator): The right shift is the number of times the data is divided by 2 power. For high-level appearance of the vacancy, the original high is what to fill this vacancy!

>>> (unsigned right-shift operator): When the data is moved to the right, the upper position appears empty, regardless of the original high, the empty space is 0 complement.

Practice:

1.//the most efficient way to figure out 2 times 8 equals a few? Answer: System.out.println (2<<3);

2. Swap the values of two integer variables?

Answer:

When developing, use the form of third-party variables because of the strong reading.
int C;
c = A;
A = b;
b = C;

Do not use this method, if the value of the two integer is too large, it will exceed the int range, will be cast. Data will
Change.

A = a + B; A = 3 + 5;a = 8;
b = a A; 3+5-5 = 3;b = 3;
A = a-B; 3+5-3 = 5;a = 5;

Use it during the interview.
A = a ^ b; A = 3 ^ 5;
b = a ^ b; b = (3^5) ^5; b = 3;
A = a ^ b; A = (3^5) ^3; A = 5;

Introduction to Java Basics essay (3) version javase

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.