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

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

高洛峰
高洛峰Original
2016-12-16 16:57:201590browse

There are three shift operators in java

06a1de183d29ac4b82bc811ec33d65c4> : : 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));
    }
}

The running result is:

1010
10100
1010

Let’s align the above results:

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

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: 5978a5c05168924abb7fe8a69100186c>,>>> To summarize related articles, please pay attention to the PHP Chinese website!

Statement:
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
Previous article:C# arrayNext article:C# array