A detailed description of the shift operation in Java (example)

Source: Internet
Author: User

1) Unsigned Right shift in Java: >>>, below is a negative number of type int, then move one bit at a time.

int k = -0x123; System.out.println (Integer.tobinarystring (k));
int n = k>>>1; System.out.println (integer.tobinarystring (n));
n = n>>>1; System.out.println (integer.tobinarystring (n));
n = n>>>1; System.out.println (integer.tobinarystring (n));
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

11111111111111111111111011011101
1111111111111111111111101101110
111111111111111111111110110111
11111111111111111111111011011
1111111111111111111111101101
111111111111111111111110110
11111111111111111111111011
1111111111111111111111101
111111111111111111111110
11111111111111111111111
1111111111111111111111
111111111111111111111
11111111111111111111
1111111111111111111
111111111111111111
11111111111111111
1111111111111111
111111111111111
11111111111111
1111111111111
111111111111
11111111111
1111111111
111111111
11111111
1111111
111111
11111
1111
111
One
1
0
0
0

2) The following is an unsigned shift for a positive number of int: Because the first digit of a positive number is a sign bit of 0, a 32-bit positive number is generally represented from the beginning of a nonzero, and the preceding 0 is omitted.

int k = 0x123; System.out.println (Integer.tobinarystring (k));
int n = k>>>1; System.out.println (integer.tobinarystring (n));
n = n>>>1; System.out.println (integer.tobinarystring (n));
n = n>>>1; System.out.println (integer.tobinarystring (n));
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

100100011

10010001

1001000

100100

10010

1001

100

10

1

0

0

0

0

3) Signed right Shift ">>", negative right shift is as follows: Each shift is removed from the rightmost one, and then the first to add a "1" to fill, so the total number of digits unchanged, is still 32 bits.

int k = -0x123; System.out.println (Integer.tobinarystring (k));
int n = k>>1; System.out.println (integer.tobinarystring (n));
n = n>>1; System.out.println (integer.tobinarystring (n));
n = n>>1; System.out.println (integer.tobinarystring (n));
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

11111111111111111111111011011101
11111111111111111111111101101110
11111111111111111111111110110111
11111111111111111111111111011011
11111111111111111111111111101101
11111111111111111111111111110110
11111111111111111111111111111011
11111111111111111111111111111101
11111111111111111111111111111110
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111

4) Signed right Shift ">>", positive numbers shift right as follows: Each shift is removed from the rightmost one, and then the first to add a "0" to fill, so the total number of digits is unchanged, is still 32 bits, but only the first is a nonzero start.

 int k = 0x123; System.out.println (Integer.tobinarystring (k));  
int n = k>>1; System.out.println (integer.tobinarystring (n));
n = n>>1; System.out.println (integer.tobinarystring (n));
n = n>>1; System.out.println (integer.tobinarystring (n));
n = n>>1; System.out.println (integer.tobinarystring (n));
n = n>>1; System.out.println (integer.tobinarystring (n));
..... ..................

100100011
10010001
1001000
100100
10010
1001
100
10
1
0
0
0

5) Move left, only the signed number is shifted to the left. Positive shift left.

int k = 0x123; System.out.println (Integer.tobinarystring (k));
int n = k<<1; System.out.println (integer.tobinarystring (n));
n = n<<1; System.out.println (integer.tobinarystring (n));
n = n<<1; System.out.println (integer.tobinarystring (n));
n = n<<1; System.out.println (integer.tobinarystring (n));
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

100100011
1001000110
10010001100
100100011000
1001000110000
10010001100000
100100011000000
1001000110000000
10010001100000000
100100011000000000
1001000110000000000
10010001100000000000
100100011000000000000
1001000110000000000000
10010001100000000000000
100100011000000000000000
1001000110000000000000000
10010001100000000000000000
100100011000000000000000000
1001000110000000000000000000
10010001100000000000000000000
100100011000000000000000000000
1001000110000000000000000000000
10010001100000000000000000000000
100011000000000000000000000000
1000110000000000000000000000000
10001100000000000000000000000000
11000000000000000000000000000
110000000000000000000000000000
1100000000000000000000000000000
11000000000000000000000000000000
10000000000000000000000000000000
0
0
0

6) Move left, only the signed number is shifted to the left. The negative number shifts to the left.

int k = -0x123; System.out.println (Integer.tobinarystring (k));
int n = k<<1; System.out.println (integer.tobinarystring (n));
n = n<<1; System.out.println (integer.tobinarystring (n));
n = n<<1; System.out.println (integer.tobinarystring (n));
n = n<<1; System.out.println (integer.tobinarystring (n));
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

11111111111111111111111011011101
11111111111111111111110110111010
11111111111111111111101101110100
11111111111111111111011011101000
11111111111111111110110111010000
11111111111111111101101110100000
11111111111111111011011101000000
11111111111111110110111010000000
11111111111111101101110100000000
11111111111111011011101000000000
11111111111110110111010000000000
11111111111101101110100000000000
11111111111011011101000000000000
11111111110110111010000000000000
11111111101101110100000000000000
11111111011011101000000000000000
11111110110111010000000000000000
11111101101110100000000000000000
11111011011101000000000000000000
11110110111010000000000000000000
11101101110100000000000000000000
11011011101000000000000000000000
10110111010000000000000000000000
1101110100000000000000000000000
11011101000000000000000000000000
10111010000000000000000000000000
1110100000000000000000000000000
11101000000000000000000000000000
11010000000000000000000000000000
10100000000000000000000000000000
1000000000000000000000000000000
10000000000000000000000000000000
0
0
0

A detailed description of the shift operation in Java (example)

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.