Home > Java > javaTutorial > Shift operators in java: <<,>>,>>> summary

Shift operators in java: <<,>>,>>> summary

高洛峰
Release: 2016-12-16 16:57:20
Original
1582 people have browsed it

There are three shift operators in java

<< : : Left shift operator, num << 1, which is equivalent to multiplying num by 2

>> : : Right shift operator, num >> ; 1, which is equivalent to dividing num by 2

>>> : Unsigned right shift, ignore the sign bit, and fill the gaps with 0

Let’s take a look at how these shift operations are used

/**
 * 
 */
package com.b510.test;

/**
 * @author Jone Hongten
 * @create date:2013-11-2
 * @version 1.0
 */
public class Test {

    public static void main(String[] args) {
        int number = 10;
        //原始数二进制
        printInfo(number);
        number = number << 1;
        //左移一位
        printInfo(number);
        number = number >> 1;
        //右移一位
        printInfo(number);
    }
    
    /**
     * 输出一个int的二进制数
     * @param num
     */
    private static void printInfo(int num){
        System.out.println(Integer.toBinaryString(num));
    }
}
Copy after login

The running result is:

1010
10100
1010
Copy after login

Let’s align the above results:

位数
--------
     十进制:10     原始数         number
     十进制:20     左移一位       number = number << 1;
     十进制:10     右移一位       number = number >> 1;
Copy after login

After reading the above demo, do you now know a lot about left shift and right shift?

For: >>>

Unsigned right shift, ignore the sign bit, and fill the gaps with 0

value >>> num -- num specifies the number of digits to shift the value value.

Just remember one thing about the rules of unsigned right shift: ignore the sign bit extension and fill the highest bit with 0. The unsigned right shift operator>>> is only meaningful for 32-bit and 64-bit values ​​


More shift operators in java: <<,>>,>>> To summarize related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template