Update 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)
Notice
In the function, the numbers N and M will given in decimal, you should also return a decimal number.
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 jand i. You would not, for example, have j=3 and i=2, because M could not fully fit between bit 3 and bit 2.
Given N=(10000000000)2, M=(10101)2, i=2, j=6
return N=(10001010100)2
分析:http://www.kancloud.cn/kancloud/data-structure-and-algorithm-notes/72988
大致步骤如下:
- 得到第
i位到第j位的比特位为0,而其他位均为1的掩码mask。 - 使用
mask与 N 进行按位与,清零 N 的第i位到第j位。 - 对 M 右移
i位,将 M 放到 N 中指定的位置。 - 返回 N | M 按位或的结果。
获得掩码mask的过程可参考 CTCI 书中的方法,先获得掩码(1111...000...111)的左边部分,然后获得掩码的右半部分,最后左右按位或即为最终结果。
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 ones = ~;
int mask = ;
if (j < ) {
int left = ones << (j + );
int right = (( << i) - );
mask = left | right;
} else {
mask = ( << i) - ;
}
return (n & mask) | (m << i);
}
};
Update Bits的更多相关文章
- Lintcode: Update Bits
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits be ...
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 解决win10无法完成更新 正在撤销更改
删除Windows 更新缓存文件按Windows+X,选择“命令提示符(管理员)”:输入:net stop wuauserv,回车(此处会提醒服务停止):输入: %windir%\SoftwareDi ...
- MD5加密详解
MD5加密详解 引言: 我在百度百科上查找到了关于MD5的介绍,我从中摘要一些重要信息: Message Digest Algorithm MD5(中文名为信息摘要算法第五版)为计算机安全领域广泛使用 ...
- C、C++的Makefile的编写以及动、静态库的制作调用(包括MAC地址的获取及MD5加密)
一.C代码 静态库 四个.h.c文件 add.h #ifndef ADD_H #define ADD_H int add(int a,int b); #endif add.c #include < ...
- MD5 32位加密算法源码(测试通过)(系转载 飞扬天下)
供自己学习使用 md5.h文件 #ifndef MD5_H #define MD5_H #include <string> #include <fstream> /* Type ...
- win10更新时遇到错误0x80070002的正确处理方法
win10更新Flash Player.或者在 “启用或关闭windows功能” 经常出现提示错误0x80070002,这要怎么解决呢?这里介绍下正确的错误代码0x80070002解决办法. 严肃提 ...
- Win10更新补丁失败后出现无法更新正在撤销 解决办法
系统更新失败,反复重启还是不行,那是不是下载下来的补丁没用了呢??所以我们先要删除Windows更新的缓存文件!在做以下操作之前,首先我们要确认系统内的windows update & BIT ...
随机推荐
- PAT 1001. A+B Format 解题
GitHub PDF 1001. A+B Format (20) Calculate a + b and output the sum in standard format -- that is, t ...
- Sprint最后一天
界面流程: 数据库里的信息: 还存在的问题: 1:选择包车城市时:下面的界面没对应到包车城市类型 2:看不到个人订票信息
- 小学生二元四则运算(F)
整体功能简介: 1.题目不重复: 2.可以定制数量: 3.可以自己选择输入范围: 4.可以选择是否添加乘除法: 5.可以选择除法结果是取整或商加余数形式表示或小数方式(默认小数点后两位)表示: 6 ...
- 第二章:蓝色巨人 IBM公司
COBOL(面向商业的通用语言) DEC(数字设备公司)华生实验室 造成失败的原因: 1,没有经营终端消费型产品 2,技术的流失 3.没有在意对手 成为的方法:合并,保守,谨慎 研究员的工作: 1,发 ...
- Linux下搭建testlink1.9.17
如果只是要搭建testlink服务的话,建议使用testlink的集成安装包,能避免很多坑 下载地址:https://bitnami.com/stack/testlink/installer 下载好后 ...
- ABP-JavaScript API
一.AJAX 1,ABP采用的方式 ASP.NET Boilerplate通过用abp.ajax函数包装AJAX调用来自动执行其中的一些步骤. 一个例子ajax调用: var newPerson = ...
- BZOJ2653 middle(二分答案+主席树)
与中位数有关的题二分答案是很常用的trick.二分答案之后,将所有大于它的看成1小于它的看成-1,那么只需要判断是否存在满足要求的一段和不小于0. 由于每个位置是1还是-1并不固定,似乎不是很好算.考 ...
- MT【170】裂项相消
已知$a,b>0$证明:$\dfrac{1}{a+2b}+\dfrac{1}{a+4b}+\dfrac{1}{a+6b}<\dfrac{3}{\sqrt{(a+b)(a+7b)}}$ 证明 ...
- NOIP2018初赛提高组复习提纲(By HGOI LJC)
Download:https://pan.baidu.com/s/16khhFf_0RsUjJLETreb20w (PDF) https://pan.baidu.com/s/1BVZqLs3q1clZ ...
- Python 进程间的通信
#-*-coding:utf-8-*- '''python提供了多种进程间的通信方式,如:Queue,Pipe,Valie+Array等. Queue与Pipe的区别在于Pipe常用来在两个进程间通信 ...