Cracking The Coding Interview5.1
//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的更多相关文章
- Cracking The Coding Interview5.2
//Given a (decimal - e.g. 3.72) number that is passed in as a string, print the binary representatio ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- 《cracking the coding intreview》——链表
前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
随机推荐
- HTML第七章总结
Getting started with CSS 前言 CSS 的 rule 作者做了一个非常形象的比喻,将 CSS 必做 renovate the house,在这里,CSS 包括了三个部分: Se ...
- 三个解释——MVC的网址
[三个MVC网址]1.https://www.cnblogs.com/sunniest/p/4555801.html2.https://www.cnblogs.com/wmyskxz/p/884846 ...
- SSH免密钥登陆
local ipaddress:10.47.39.7:remote ipaddress:10.47.39.8 1.生成公钥和私钥 [root@local ~]# ssh-keygen -t rsa ...
- 关于java和c语言中,变量重名问题
请看下面的两段代码: #include <iostream>#include <cstdio>#include <cstring> using namespace ...
- MySQL表类型和存储引擎
一.基本介绍 从事务安全性的角度,可以把存储引擎分为两大类: 事务安全: BDB和innodb; 事务非安全性: myisam 和 memory 二.存储引擎的比较图 看你的mysql当前默认的存储引 ...
- wordpress +window 走起~
一.安装XAMPP 1 百度搜索xampp后下载安装到D盘(安装时按默认勾选安装即可) 2 安装完后,点击Start来启动Apache和MySQL这两个服务 3 如果Apache服务不能启动,多数 ...
- 【oauth2.0】【2】JAVA 客户端模式
含义:用户直接向客户端注册,客户端以自己的名义要求"服务提供商"提供服务,其实不存在授权问题 步骤: (A)客户端向认证服务器进行身份认证,并要求一个访问令牌(token). (B ...
- Convert PIL Image to byte array?
1.import io img = Image.open(fh, mode='r') roiImg = img.crop(box) imgByteArr = io.BytesIO() roiImg.s ...
- tornado 异步
引言 注:正文中引用的 Tornado 代码除特别说明外,都默认引用自 Tornado 4.0.1. tornado.gen 模块是一个基于 python generator 实现的异步编程接口.通过 ...
- python 常用代码
获取标签名 h1 class 是h1usersoup.find(name="h1", attrs={"class":"h1user"});获 ...