5.1---二进制数插入(CC150)

public class Solution {
    public static int binInsert(int n, int m, int i, int j) {
        // write code here
//        return n | (m << i);
        int allOnes = ~0;
        int left = allOnes << (j + 1);
        int right = (1 << i) - 1;
        
        int mask = left | right;
        int n_cleared = mask & n;
        int m_shifted = m << i;
       return n_cleared | m_shifted;
    }
    public static void main(String[] args){
        System.out.println(binInsert(4,1,1,1));
    }
}
原文地址:https://www.cnblogs.com/yueyebigdata/p/5072683.html