//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. Win7无法保存共享帐户密码

    每次机器重启完之后,网络共享的密码总是要重新输入. [记住我的凭据]选项不起作用. 查到了下面百度经验的文章,挺靠谱的. https://jingyan.baidu.com/article/59a01 ...

  2. MySQL Server and Server-Startup Programs

    1. mysqld-The MySQL Server mysqld,also known as mysql server, is the main program that does most of ...

  3. 数据结构(C语言版)-第4章 串、数组和广义表

    补充:C语言中常用的串运算 调用标准库函数 #include<string.h> 串比较,strcmp(char s1,char s2) 串复制,strcpy(char to,char f ...

  4. 《HTTP 权威指南》笔记:第十二章 基本认证体制

    导言 客户端可以通过网络来得到想要的信息,但是有一些信息并不能是对所有人都能看到的,因此必须有一种认证机制.服务器需要通过这种方式来了解用户身份,一旦服务器知道了用户的身份,就可以让用户能够访问请求的 ...

  5. “安利”一个CDN服务商网站

    一.CDN简介 CDN的全称是Content Delivery Network,即内容分发网络.CDN是构建在网络之上的内容分发网络,依靠部署在各地的边缘服务器,通过中心平台的负载均衡.内容分发.调度 ...

  6. fedora21 中lamp的搭建(测试没有问题)

    LAMP Stands for Linux,Apache,MySQL and PHP. Most of the websites works with the above combination. T ...

  7. python的索引问题

    之前自己在python索引中一直遇到这样的问题: data = np.arange(12).reshape((3,4)) print(data[:][0]) 想要索引第一列时总是索引到第一行,后来发现 ...

  8. javascript之动画特效

    JavaScript的动画用的最多的3个api就是setInterval().setTimeout()和requestAnimationFrame()

  9. python中lambda的用法

    一.lambda函数也叫匿名函数,即,函数没有具体的名称.先来看一个最简单例子: def f(x):return x**2print f(4) Python中使用lambda的话,写成这样 g = l ...

  10. hdu-6435

    Problem J. CSGO Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others ...