首页 > 其他分享 >lintcode:Update Bits

lintcode:Update Bits

时间:2022-12-01 19:06:22浏览次数:36  
标签:return mask int bits lintcode Update between bit Bits


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)

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.

这题如果转化为二进制字符串再操作不难。
但如何用位操作来达到目标比较难。

  • 如果j<31,即j不在最高位上。可以把i到j位清为0,可以​​((1<<(j+1))-(1<<i))​​得到i到j之间全是1的数,再取反,得到i到j之间全是0的数。
  • 如果j=32,​​(1<<(j+1))即(1<<32),相当于1<<1​​​ 不可行。可以直接​​(1<<i)-1​​ 得到i到j之间全是0,其他地方是1的数。
  • 上面得到的数成为掩码
  • ​(m<<i)+(n&mask)​​ 可以得到最终解。
class Solution {
public:
/**
*@param n, m: Two integer
*@param i, j: Two bit positions
*return: An integer
*/
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)+(n&mask);

}
};


标签:return,mask,int,bits,lintcode,Update,between,bit,Bits
From: https://blog.51cto.com/u_15899184/5903598

相关文章

  • lintcode: Fast Power
    Calculatethea^b%bwherea,bandnareall32bitintegers.ExampleFor231%3=2For1001000%1000=0ChallengeO(logn)思路参见我的博客​​幂模运算​​cla......
  • lintcode:Binary Tree Serialization
    Designanalgorithmandwritecodetoserializeanddeserializeabinarytree.Writingthetreetoafileiscalled‘serialization’andreadingbackfromthe......
  • lintcode: N-Queens
    Then-queenspuzzleistheproblemofplacingnqueensonann×nchessboardsuchthatnotwoqueensattackeachother.Givenanintegern,returnalldistincts......
  • lintcode:Permutations
    Givenalistofnumbers,returnallpossiblepermutations.ChallengeDoitwithoutrecursion.1.递归classSolution{public:/***@paramnums:Alistofi......
  • lintcode:Subsets
    Givenasetofdistinctintegers,returnallpossiblesubsets.ChallengeCanyoudoitinbothrecursivelyanditeratively?1.18sclassSolution{public:/**......
  • lintcode: Permutations II
    Givenalistofnumberswithduplicatenumberinit.Findalluniquepermutations.可以见我的博文​​全排列实现​​classSolution{public:/***@paramnu......
  • lintcode: Subsets II
    Givenalistofnumbersthatmayhasduplicatenumbers,returnallpossiblesubsets1.先排序;再按求Subsets一样的做法,只是添加前检查是否已经存在。耗时171mscla......
  • lintcode:Subarray Sum Closest
    Givenanintegerarray,findasubarraywithsumclosesttozero.Returntheindexesofthefirstnumberandlastnumber.ExampleGiven[-3,1,1,-3,5],retur......
  • lintcode: Sqrt(x)
    Implementintsqrt(intx).Computeandreturnthesquarerootofx.Examplesqrt(3)=1sqrt(4)=2sqrt(5)=2sqrt(10)=3ChallengeO(log(x))求非线性方程的解可以......
  • gitee推送更新失败问题记录:remote: error: hook declined to update refs/heads/maste
    问题描述:gitee推送更新时,提示:remote:PoweredbyGITEE.COM[GNK-6.4]remote:error:GE007:Yourpushwouldpublishaprivateemailaddress.    remote:......