//You are 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:
//
//Input: N = 10000000000, M = 10101, i = 2, j = 6
//
//Output: N = 10001010100 #include <iostream>
#include <vector>
using namespace std;
/***纯粹是计算题***/
int mset(int N, int M, int i, int j)
{
int t = 1;
t =t << j-i+1;
t --;//得到0000 0000 0001 1111
t=t<<i;
t = ~t;//得到1111 1111 1100 00011
M= M<<i;
int K = t & N;//将N对应位置set为0
K = K|M;
return K;
} void print(int p)
{
vector<int> v;
for (int k = 0;k<32; k++)
{
int t = p&1;
v.push_back(t);
p=p>>1;
}
for (int i = v.size()-1;i>-1; i--)
{
cout<<v[i]<<" ";
}
cout<<endl;
}
int main()
{
int n = 1<<10, m = 21;
print(n);
print(m);
cout<<"====================================================================="<<endl;
for (int t = 0;t<20;t++)
{
int ss= mset(n, m,t,t+4);
print(ss);
} return 0;
}

Cracking The Coding Interview5.1的更多相关文章

  1. Cracking The Coding Interview5.2

    //Given a (decimal - e.g. 3.72) number that is passed in as a string, print the binary representatio ...

  2. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  3. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  4. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  5. 《cracking the coding intreview》——链表

    前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...

  6. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  7. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  8. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  9. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

随机推荐

  1. Ubuntu更改源和搜狗输入法安装卸载

    安装完Ubuntu 16.04后,要更换为国内的软件源: sudo gedit /etc/apt/sources.list   #用文本编辑器打开源列表 在文件开头添加下面的阿里云的软件源: deb ...

  2. 动手动脑java异常处理

    1>请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. import javax.swing.*; class AboutEx ...

  3. Confluence 6 自定义你的空间

    通过对你的空间进行界面的自定义能够让你的空间更加出类拔萃. 如果你具有空间管理员权限,你可以修改你空间的颜色配色,添加你自己的空间标识,选择是否在你空间中显示边栏.或者你可以进入 Atlassian ...

  4. 3 爬虫解析 Xpath 和 BeautifulSoup

    1.正则表达式 单字符: . : 除换行以外所有字符 [] :[aoe] [a-w] 匹配集合中任意一个字符 \d :数字 [-] \D : 非数字 \w :数字.字母.下划线.中文 \W : 非\w ...

  5. lanmp环境中php版本的升级为7.1

    查看php版本的信息 vim  ./lib/phps.sh 设置权限   chmod 755 ./lib/phps.sh 下载版本  ./lib/phps.sh  7.1.4 查看版本 php -v ...

  6. 自定义session的存储机制

    <?php class MSession implements SessionHandlerInterface{ // reids 对象 protected $handler = null; / ...

  7. 『TensorFlow』SSD源码学习_其四:数据介绍及TFR文件生成

    Fork版本项目地址:SSD 一.数据格式介绍 数据文件夹命名为VOC2012,内部有5个子文件夹,如下, 我们的检测任务中使用JPEGImages文件夹和Annotations文件夹. JPEGIm ...

  8. 自定义alert弹框,title不显示域名

    问题: 系统默认的alert弹框的title会默认显示网页域名 解决办法: (修改弹框样式) (function() { window.alert = function(name) { $(" ...

  9. 关于final static修饰的常量部署后没有更新的问题

    出现问题的场景是这样的: 项目中有个专门放流程Key值常量的类FlowConstants.java,其中这些常量都用了final static 修饰.某天因为修改了流程,相应的key值也改变了,所以直 ...

  10. [CodeForces - 447A] A - DZY Loves Hash

    A - DZY Loves Hash DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert ...