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 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.
以题中例子为例,做一个滤波器在i,j之间:11110000011,来跟N按位与,再把M左移i位,按位或
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 len = j-i+1;
int temp = 0;
for (int x=0; x<len; x++) {
temp |= 1<<x;
}
temp = ~(temp<<i);
return (n&temp) | (m<<i);
}
}
Lintcode: 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 ...
- Lintcode: Flip Bits
Determine the number of bits required to flip if you want to convert integer n to integer m. Have yo ...
- 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]——目录
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解决办法. 严肃提 ...
随机推荐
- taocode
http://code.taobao.org/project/lang/list/Go/1/
- Redis学习笔记(1)-Key
package cn.com; import java.text.ParseException; import java.util.List; import java.util.Set; import ...
- Erlang ERTS的Trap机制的设计及其用途
出处:http://mryufeng.iteye.com/blog/334744 erlang的trap机制在实现中用的很多,在费时的BIF操作中基本上都可以看到.它的实现需要erl vm的配合.它的 ...
- Cocos2d-JS工程中的文件结构
res文件夹存放资源文件 src文件夹是主要的程序代码 app.js是实现游戏场景的JavaScript文件 resource.js在src文件夹中,定义资源对应的变量 config.json保存模拟 ...
- QComboBox
#include "dialog.h" #include "ui_dialog.h" #include <QtCore> #include < ...
- 20145211 《Java程序设计》第1周学习总结——小荷才露尖尖角
教材学习内容总结 Java语言概述 Java是SUN1995年推出的一门高级编程语言,完全面向对象,安全可靠,具有跨平台性(用其编写的语言在任何系统上都能运行,只需安装一个JVM) Java三大平台包 ...
- [LeetCode]题解(python):045-Jump game II
题目来源 https://leetcode.com/problems/jump-game-ii/ Given an array of non-negative integers, you are in ...
- [LeetCode]题解(python):052-N-Queens II
题目来源 https://leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem. Now, instead outputt ...
- ASP.NET 开发笔记1
1.GirdView 动态添加列 PostBack 后 模板列中的控件丢失的问题 http://blackboy51.blog.163.com/blog/static/511359122011910 ...
- JQuery 方法简写
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...