Java Foundation--1--operator

Source: Internet
Author: User

One, bitwise operators

Any information is stored in binary form on the computer, "&", "|", "^" can be used as a bitwise operator in addition to being a logical operator.

&: Only the two bits that participate in the operation are 1,& and the result is 1, otherwise 0.

|: Only the result of the 0,| operation on both sides of the participating operation is 0, otherwise 1.

^: The two sides of the join operation are the same 0, the difference is 1.

~: Anti-code;

A number of different or the same number two times, the result is that number. Use a simple encryption idea. (Encrypt image code:)

ImportJava.io.*;classdemo4{ Public Static voidMain (string[] args)throwsException {File inFile=NewFile ("D:\\1.gif"); File OutFile=NewFile ("d:\\ encrypted. gif"); FileInputStream input=NewFileInputStream (InFile); FileOutputStream Output=NewFileOutputStream (OutFile); intContent=0;  while((Content=input.read ())!=-1) {output.write (content^12);        } output.close ();    Input.close (); }} Encryption

Written question: Exchange two variables without using a third-party variable. (with ^)

class Demo5 {    publicstaticvoid  main (string[] args)     {        int a=3;         int b=5;        A=a^b;        b=a^b;        A=a^b;        System.out.println (a+ "," +B);    }} Swap variables
View Code

Second, shift operator

1. Shift Left (arithmetic shift "<<"): Empty 0, number of digits exceeded, discard.

Conclusion: The left shift is the equivalent of multiplying by 2 of the n-th power.

2. Move right (">>") shifted binary highest bit is 0, right shift, vacancy bit 0, highest bit is 1, vacancy is 1;

Conclusion: Right shift is equivalent to dividing by 2 n Power, n is the number of shifts.

3. Unsigned right Shift (">>>") right shift number of digits to the left of the operand (highest bit) uniform with 0 complement.

Third, operator order

Java Foundation--1--operator

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.