Remove the last 1 of the integer binary in Java (bit operation)

n&(n-1)To remove 二进制the last of the integers1

Reference Code

public class Test {
    
    
    public static void main(String[] args) {
    
    
        // 5的二进制表示为:101
        // 去掉二进制最后一个1,则为:100
        // 转换成10进制为:4
        System.out.println(5&(5-1)); // 4
        // 1010
        System.out.println(10&(10-1)); // 8
		// 1001
		System.out.println(9&(9-1)); // 8
    }
}

to sum up

位运算Try to use C/C++language as much as possible , not in Java 无符号整数类型.

Guess you like

Origin blog.csdn.net/Awt_FuDongLai/article/details/112347109