Dark Horse Programmer--java Basics-operator, bit manipulation

Source: Internet
Author: User
Tags bitwise bitwise operators

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

<pre name= "code" class= "Java" >public class test{public    static void Main (String args[]) {          byte b = 3;          B = B + 2;          System.out.println (b);}}

This code output is wrong, because byte accounted for a byte, no defined integer 2 accounted for 4 bits, b+2-phase enhancement to int type, and then assigned to B, but B only 1 bits, so will error, the correct code:

public class test{public    static void Main (String args[]) {          byte b = 3;          b = (int) (b + 2);          System.out.println (b);}}
The difference between 2.system.out.println and System.out.print is that the former contains a \ n transfer character and wraps itself.

Several escape characters common to Java:

\ n: Line break

\b: Backspace

\ t: tab, equivalent to table

\ r: Enter, equal to \ n

3. Operators

① arithmetic operator +,-,*,/,%

Emphasize two operators:/and%

eg

int x = 4250;

x = x/1000*1000;

The result is not 4250, but 4000.

The% operator cannot have decimals, and the sign of the result when remainder depends on the preceding number.

1%-5 = 1;

-1%5 =-1;

* It is good to avoid overflow when using the method operator.

Special emphasis is placed on the + + and--operator, a++,++a,b--,--b;

The operator is preceded by the assignment of a value, and in the following case it is assigned after the first operation.

②: Logical operators & | ^ ! &&| |

&& | | Also known as short-circuit operator

Differences between &&, &, | |, | operators: A (&&, &, | |, |) B

&& when A is false, B does not have to judge, | | When A is true, B does not have to judge, | and & whether A is true or False B is judged.

③: Link operator +

The + in Ava represents the link operator, and when the string is connected to any data, it eventually becomes a string.

④: Bitwise operators << >> ^ >>> & | !

>> signed Right Shift

>>> Unsigned Right Shift

The bitwise operator is computationally efficient, and when the same number is two times this value does not change, eg7^4^4=7.

Two number exchange:

Method One: Variable method

temp = A;

A = b;

b = temp;

Method Two:

A = a +b;

b = a A;

A = a-B;

Method Three:

A = a ^ b;

b = a^b;

A = A^b;

>> operator: If it is a negative number, then the highest bit of the bit is basically 1, of course, by the original highest bit, so the operation often uses >>>.

Take a minimum of four bits of a number with a minimum of four bits of &,eg:60 of 1100,

You can use 60&15, 0011 1100

0000 1111

------------------

0000 1100

If it is the second-to-last four-bit, you can take the lowest four, and then &, how to go, of course, the right SHIFT, >>> 4 & 15.

0011 1100

>>> 0000 0011

& 0000 1111

-------------------------

0000 0011

That's OK.



Dark Horse Programmer--java Basics-operator, bit manipulation

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.