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.

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 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.

Example

Given N=(10000000000)2M=(10101)2i=2j=6

return N=(10001010100)2

分析:http://www.kancloud.cn/kancloud/data-structure-and-algorithm-notes/72988

大致步骤如下:

  1. 得到第i位到第j位的比特位为0,而其他位均为1的掩码mask
  2. 使用mask与 N 进行按位与,清零 N 的第i位到第j位。
  3. 对 M 右移i位,将 M 放到 N 中指定的位置。
  4. 返回 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的更多相关文章

  1. 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 ...

  2. LintCode刷题笔记-- Update Bits

    标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. 解决win10无法完成更新 正在撤销更改

    删除Windows 更新缓存文件按Windows+X,选择“命令提示符(管理员)”:输入:net stop wuauserv,回车(此处会提醒服务停止):输入: %windir%\SoftwareDi ...

  5. MD5加密详解

    MD5加密详解 引言: 我在百度百科上查找到了关于MD5的介绍,我从中摘要一些重要信息: Message Digest Algorithm MD5(中文名为信息摘要算法第五版)为计算机安全领域广泛使用 ...

  6. 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 < ...

  7. MD5 32位加密算法源码(测试通过)(系转载 飞扬天下)

    供自己学习使用 md5.h文件 #ifndef MD5_H #define MD5_H #include <string> #include <fstream> /* Type ...

  8. win10更新时遇到错误0x80070002的正确处理方法

    win10更新Flash Player.或者在  “启用或关闭windows功能” 经常出现提示错误0x80070002,这要怎么解决呢?这里介绍下正确的错误代码0x80070002解决办法. 严肃提 ...

  9. Win10更新补丁失败后出现无法更新正在撤销 解决办法

    系统更新失败,反复重启还是不行,那是不是下载下来的补丁没用了呢??所以我们先要删除Windows更新的缓存文件!在做以下操作之前,首先我们要确认系统内的windows update & BIT ...

随机推荐

  1. LeetCode 88. 合并两个有序数组

    题目: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  2. 03-java学习-基本数据类型-运算符-键盘接收用户输入

    java的八大基本数据类型: 类型转换的基本原则: java整数的默认类型是int,小数的默认类型是double 运算符: 算术运算符.连接.赋值.关系.逻辑.三目运算符等…… 键盘接收用户输入: j ...

  3. OS X(10.10) python3.4 matplotlib的安装

    最近在用python做一些数据处理相关的工作,当然少不了matplotlib这个模块.之前在windows下分分钟安装成功,结果到了mac下死活编译不过去. 最后还是在stackoverflow上找到 ...

  4. 助教日志—请沈航13级同学将GIT地址和CNBLOG地址发到这篇博文的评论中

    一.评论形式: 学号 姓名 博客地址 GIT地址 如 2011102456 郑蕊 http://www.cnblogs.com/zhengrui0452/ http://121.42.14.1/Rui ...

  5. CentOS75 安装Oracle18c

    1. 参考地址 https://blog.csdn.net/u010257584/article/details/50902472https://www.cnblogs.com/kerrycode/a ...

  6. java8新特性(四)_Stream详解

    之前写过一篇用stream处理map的文章,但是对stream没有一个整体的认识,这次结合并发编程网和ibm中介绍stream的文章进行一个总结,我会着重写对list的处理,毕竟实际工作中大家每天进行 ...

  7. JDK & Eclipse & SVN

    JDK & Eclipse & SVN Mac 开机密码 commit SVN for MacOS Java SE 11.0.2(LTS) https://www.oracle.com ...

  8. boost.asio学习-----reslover 域名解析

    将域名解析为ip地址并输出: #include "stdafx.h" #include "boost/asio.hpp" #include <boost/ ...

  9. BZOJ 4173: 数学

    4173: 数学 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 462  Solved: 227[Submit][Status][Discuss] D ...

  10. JVM总结(一):概述--JVM对象探秘

    这一节我们来讨论一下JVM对象建立过程. JVM对象探秘 对象的建立 对象的内存布局 对象的访问定位 JVM对象探秘 对象的建立 对象的建立过程   图一:对象建立过程 1.类加载检查. 当JVM检测 ...