Java shift operators and assignment operators __java programming experience

Source: Internet
Author: User
A. Shift operatorThe shift operator is also for the binary "bit", which mainly includes the left shift operator (<<), the right shift operator (>>>), and the signed right shift operator (>>). Where: The left-shift operator is represented by "<<", which is the object to the left of the operator, the number of digits to the right of the left operator, and the low 0. In fact, to move n bit to the left, it is equivalent to multiply by 2 n times. The right shift operator, represented by the symbol ">>>", is the specified number of digits to the right of the object to the left of the operator, and at the top of 0, in fact, the right n bit, which is equal to the n-th side of the 2. The signed right shift operator is represented by the symbol ">>", which is the operand to the left of the operator, and the specified number of digits to the right of the operator. If it is a positive number, fill 0 in the high, or 1 in the high position if it is negative.
public class data17{
public static void Main (string[] args) {
int a=2; int b=2;
int x=16; int y=2;
int m=16;    int n=2;    int p=-16; int q=2;

The result of System.out.println ("A shift is:" + (a<<b));
SYSTEM.OUT.PRINTLN ("The result of the x shift is:" + (x>>>y));
System.out.println ("M's shift Result:" + (m>>n));
System.out.println ("P's shift Result:" + (P&GT;&GT;Q));
}
}

Run Result:
The result of a shift is: 8
The results of the x shift are: 4
Shift result of M: 4
Shift result of P:-4 two. Assignment operatorAn assignment is a value assigned to a variable, and the assignment operator acts as the assignment, but the simplest assignment operator is "=".

There are, of course, many other assignment operators besides "=". There are "+ =", "=", "*=", "/=", "%=", ">>=", ">>>=", "<<=", "&=", "|=", "^=". Here's a simple example.

public class data20{
public static void Main (string[] args) {
int a=5;
int b=2;
System.out.println ("A+=b Value:" + (a+=b));
System.out.println ("A-=b Value:" + (a-=b));
System.out.println ("A*=b Value:" + (a*=b));
System.out.println ("A/=b Value:" + (a/=b));
System.out.println ("A%=b Value:" + (a%=b));
System.out.println ("A>>=b Value:" + (a>>=b));
System.out.println ("A>>>=b Value:" + (a>>>=b));
System.out.println ("A<<=b Value:" + (a<<=b));
System.out.println ("A&=b Value:" + (a&=b));
System.out.println ("A|=b Value:" + (a|=b));
System.out.println ("A^=b Value:" + (a^=b));
}
}

Output results:
"A+=b" is a=a+b=7;
"A-=b" is a=a-b=7-2=5;
"A*=b" is a=a*b=5*2=10;
"A/=b" is a=a/b=10/2=5;
"A%=b" is a=a%b=5%2=1;
"A>>=b" is a=a>>b=0;.
"A>>>=b" is a=a>>>b=0;.
"A<<=b" is a=a<<b=0;.
"A&=b" is a=a&b=0;.
"A|=b" is a=a|b=2;
"A^=b" is a=a^b=0;

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.