Java Basic Three

Source: Internet
Author: User

One, bitwise operators

The previously learned operators are either variable-based or constant.

Example 1:

Class test1{

public static void Main (String [] args) {

& | ^    ~

All four of the above operators are bit-based.

&: Each one compares, there are 0 0, no 0 1.

// | : Each person compares, there are 1 1, No 1 0.

^:, same is 0, different 1.

~: Each one is reversed.

System.out.println (6&3);

System.out.println (6|3);

System.out.println (6^3);

System.out.println ();

}

}

/*

============================

110

& 011

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

010

============================

110

| 011

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

111

============================

110

^ 011

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

101

============================

£ º

int four bytes

00000000 00000000 00000000 00000110 6 The complement of the computer is calculated in the form of a complement

11111111 11111111 11111111 111111001 the complement

Seek source 1000000 00000000 00000000 00000111 -7

*/

Example 2:

Class operator_demo2{
public static void Main (String [] args) {
Two number exchange, third variable not applicable
int x=10;
int y=5;
/*
int temp=0;
temp=x;//temp=10
X=y;//x=5
y=temp;//y=10
System.out.println (x+ ":" +y);
*/

/*
If you put two numbers in and out, put them in one, and then, using and with one number can ask for another number.
X=x+y;//x=15
Y=x-y;//y=15-5=10= the original X
X=x-y;//x=15-10=5= the original Y
System.out.println (x+ ":" +y);
*/

^: There is a characteristic of XOR operation, one data is different to another data or two times, the data is unchanged
System.out.println (5^10^10);
System.out.println (5^10^5);
0^0=0 0^0=0
1^0=1 1^0=1
0^1=1 1^1=0
1^1=0 0^1=1

Difficulty: Using ^, swap x with Y.
X=x^y; x=10^5;
Y=x^y; y=10^5^5=10;
X=x^y; x=10^5^10=5;
System.out.println (x+ ":" +y);

}
}

Example 3:

Class test2{

Pubic static void Main (String [] args) {

/*

<<: Left shift, left highest bit discarded, right 0

eg:0000 0001, 0000 0010

>>: Move right, the highest is 0, the left side is 0, the highest is 1, the left is 1.

>>>: Unsigned Right Shift: 0 on the left, whether the highest bit is 0 or 1.

*/

System.out.println (12<<1); 1 Power of 12*2 24

System.out.println (12<<2); 2 Power of 12*2 48

/*
00000000 00000000 00000000 00001100 12 complement
(0) 000000000000000 00000000 000011000 8+16=24
(00) 000000 00000000 00000000 0000110000 16+32=48
*/

System.out.println (12>>1); 12/2 of 1 Power 6
System.out.println (12&GT;&GT;2); 12/2 of 2 Power 3
System.out.println (12>>3); 1
System.out.println ( -2>>1); -1
/*
00000000 00000000 00000000 00001100 12 complement
000000000 00000000 00000000 0000110 (0) 4+2=6
0000000000 00000000 00000000 000011 (00) 2+1=3
00000000000 00000000 00000000 00001 (100) 1=1

10000000 00000000 00000000 00000010 Original code-2
11111111 11111111 11111111 11111110 complement plus one
111111111 11111111 11111111 1111111 (0) Complement of results
100000000 00000000 00000000 0000001 The original code of the result =-1
*/

Unsigned Right Shift
System.out.println ( -2>>>1); 2147483647
/*
11111111 11111111 11111111 11111110 complement
011111111 11111111 11111111 1111111 (0) Complement of results

*/
The Write program calculates 2 of the 8-time Square
System.out.println (2*2*2*2*2*2*2*2);
General Programmer: Loop *
High-end programmers
System.out.println (1&LT;&LT;8);

}

}

II. Structure of the procedure

The structure of sequential structure and condition, and the structure of circulation

Example 1:

Conditional branching Structure-if
If statement syntax format:
/*
if (comparison expression) {
Statement body;
}
Run the process:
Evaluates the value of the comparison expression first to see if the return value is TRUE or False
If true, the statement body is executed.
If False, the statement body is not executed.

*/

Class if_demo1{
public static void Main (String [] args) {
int a=3;
if (a<2) {
System.out.println ("After comparison, A is less than 2");
}
SYSTEM.OUT.PRINTLN ("program execution Complete");
}
}

Example 2:

1, the value of the comparison expression, whether simple or complex, the result must be a Boolean type.
2. When the statement body controlled by the IF statement is a statement, the curly braces can be omitted
3. If the statement body controlled by the IF statement is more than one statement, the curly braces must be written
4, * * * requirements, whether the statement body is one or more, we unified enlarged brackets!

Class if_demo2{
public static void Main (String [] args) {
int age=17;
if (age>=8&&age<18); This statement is the right one.
System.out.println ("User belongs to the anti-addiction group");

}
}

Java Basic Three

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.