7.Java shift operators and assignment operators

Source: Internet
Author: User

I. SHIFT operators

The shift operator is also for binary "bit", which mainly includes: Left shift operator (<<), right shift operator (>>>), signed right shift operator (>>). which

    • The left shift operator is denoted by "<<", which is the object to the left of the operator, the number of bits specified to the right of the operator to the left, and 0 in the low. In fact, moving the N-bit to the left is equivalent to multiplying by 2 of the n-th square.
    • The right-shift operator is denoted by the symbol ">>>", which is the number of bits specified to the right of the operator to the left of the operators, and 0 on the high, in fact the right shift n bits, which is equivalent to the nth of 2.
    • The signed right-shift operator, denoted by the symbol ">>", is the operand to the left of the operator, and the number of digits specified to the right of the operator to the right. If it is positive, fill 0 in the high position, and if it is negative, then fill 1 in the high position.


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;

SYSTEM.OUT.PRINTLN (the result of a shift is: "+ (a<<b));
SYSTEM.OUT.PRINTLN ("x shift result is:" + (x>>>y));
SYSTEM.OUT.PRINTLN ("Shift result of M:" + (m>>n));
System.out.println ("p shift Result:" + (P>>Q));
}
}

Operation Result:
The result of a shift is: 8
The result of the x shift is: 4
Shift results for M: 4
Shift results for P: 4

Two. Assignment operators

Assignment is assigning a value to a variable, and the assignment operator acts as the assignment, in fact, 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 Result:
"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;

7.Java shift operators and assignment operators

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.