Update Bits
来源:互联网 发布:mac哑光修容粉怎么选 编辑:程序博客网 时间:2024/05/18 13:25
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e g , M becomes a substring of N located at i and starting at j)
Have you met this question in a real interview? Yes
Example
Given N=(10000000000)2, M=(10101)2, i=2, j=6
return N=(10001010100)2
Note
In the function, the numbers N and M will given in decimal, you should also return a decimal number.
Challenge
Minimum number of operations?
Clarification
You can assume that the bits j through i have enough space to fit all of M. That is, if M=10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j=3 and i=2, because M could not fully fit between bit 3 and bit 2.
class Solution { /** *@param n, m: Two integer *@param i, j: Two bit positions *return: An integer */ public int updateBits(int n, int m, int i, int j) { // write your code here int mask; if (j < 31) { mask = ~((1 << (j + 1)) - (1 << i)); } else { mask = (1 << i) - 1; } return (m << i) + (mask & n); }}
- Update Bits
- lintcode:Update Bits
- #179 Update Bits
- Lintcode179 Update Bits solution 题解
- 重装windows update服务和BITS服务
- 179.Update Bits-更新二进制位(中等题)
- Bits
- Bits*
- update
- update
- update
- update
- update
- update
- update
- update
- update
- update
- Android HTTPS认证
- 7.Active Record
- 【转】HTTP 头部解释
- 正则表达式
- Dalvik与ART相关源代码分析
- Update Bits
- spring提供的三种定时任务
- vs2012配置ffmpeg
- 居于H5的多文件、大文件、多线程上传解决方案
- CAS 单点登录实战 (2) 服务器端
- HDU 1114 Piggy-Bank
- 将Android Studio的设置恢复到初始化
- 为什么Java中1000==1000为false而100==100为true?
- 使用Doxygen为VHDL工程生成文档模板