003 operator,

Source: Internet
Author: User

003 operator,

Content: auto-incrementing operator, right-shift operator, logical operator, and or operator, XOR operator, connector, exercise (Solution of the odd way)

For reference by a few people

####################################

Auto-increment operator

X ++: Indicates x auto-increment 1.

The following are common hybrid points:

Int a = 6, B; B = a ++; // The result is B = 6, a = 7.

The above code is equivalent

Temp = a; // temporarily store a = a + 1; // then a auto-increment B = temp; // assign the temporary value to B

Focus on B = a ++. Remember that the value assigned by B is the value before a is not auto-incrementing.

#####################################

Shift right OPERATOR: >>, unsigned shift right: >>>

The storage of integer characters is 8 bits, for example, 9. The binary storage is 0000 1001. After the right shift operator 1 is 9> 1, it is changed to 1000 0100, that is, the next 1 is moved to the front.

Unsigned shift 1-9> 1: 0000 0100

########################################

Logical OPERATOR: & |||<>=

& Difference: & whether the operations on both sides are true, & if the left side is false, no operation on the right side; | and |: true on the left side, no operation on the right side, if the value on the left is false, the operation continues on the right.

######################################## #############

And or non-OPERATOR: & | distinguishes it from the preceding logical operator

Binary operations

0010 1011 0010 1011

& Amp; 0000 0111 | 0000 0111

= 0000 0011 and the operation, two are 1 to 0010 1111 or the operation, one is 1 and the result is 1

######################################## ##############

XOR operator: ^

6 ^ 3 = 0110 ^ 0011 = 0101 to reduce the abbreviation, I will not write the previous four zeros. Is a binary operation

It is worth noting that this feature is used in 6 ^ 3 ^ 3 = 6 6 6 ^ 3 ^ 6 = 3 exercises.

######################################## #################

Connector: Pay attention to the output

Int a = 3, B = 4; System. out. println ("5 + 5 =" + 5 + 5); // 5 + 5 = 55System. out. println ("5 + 5 =" + (5 + 5); // 5 + 5 = 10System. out. println ("a =" + a + ", B =" + B); // a = 3, B = 4
// String connection is easy to use and will not be explained

 

####

Value assignment operator.
+ =; Automatic type conversion may occur
Short s = 4;
S = s + 5; // compilation fails. Because int and short types are added on the right, different types cannot be added.
S + = 5; // After compilation, the type conversion occurs. The short type is automatically converted to the int type, so it can be added. If you cannot understand it, remember/funny.

 

######################################## ######################################## ####################

Exercise

1. Calculate 2x8 in the most efficient way

2. Swap the values of Two integer variables (not through the third variable)

System.out.println((2<<3))
Answer 1
int a = 5,b = 6;a = a + b;    //a = 5+6 = 11b = a - b;    // b = 11-6 = 5a = a - b;    // a = 11-5 = 6
Answer 2.1
Int a = 5, B = 6; a = a ^ B; // a = 5 ^ 6 (a ^ B) B = a ^ B; // B = 5 ^ 6 ^ 6 (a ^ B) a = a ^ B; // a = 5 ^ 6 ^ 5 at this time, B has been changed to 5 after the previous operation.
Answer 2.2

Note that the two methods should not be out of the Integer Range.

 

If you have any questions, please comment below and I will reply.

 

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.