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));
    }
}

 

posted @ 2015-12-24 12:46  创业-李春跃-增长黑客  阅读(335)  评论(0编辑  收藏  举报