博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Cracking the Coding Interview》——第5章:位操作——题目1
阅读量:4587 次
发布时间:2019-06-09

本文共 980 字,大约阅读时间需要 3 分钟。

2014-03-19 05:45

题目:给定两个数M和N,将N按照二进制位,覆盖到M的特定段位中去。

解法:位操作,请看代码。

代码:

1 // 5.1 Insert one number into the certain bit segment of another number. 2 #include 
3 using namespace std; 4 5 void printBinary(unsigned num) 6 { 7 unsigned bit = 1 << 31; 8 9 do {10 putchar('0' + !!(num & bit));11 bit >>= 1;12 } while (bit);13 }14 15 unsigned insertBits(unsigned n, unsigned m, int ll, int rr)16 {17 return (n >> (rr + 1) << (rr + 1)) + (m << ll) + (n - (n >> ll << ll));18 }19 20 int main()21 {22 unsigned n, m;23 unsigned res;24 int low, high;25 26 while (scanf("%u%u", &n, &m) == 2) {27 scanf("%d%d", &low, &high);28 res = insertBits(n, m, low, high);29 printBinary(n);30 putchar('\n');31 printBinary(m);32 putchar('\n');33 printBinary(res);34 putchar('\n');35 }36 37 return 0;38 }

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3610483.html

你可能感兴趣的文章
jQuery实现鼠标滑过图片列表加遮罩层
查看>>
IOS Get 请求
查看>>
Unique Binary Search Trees @leetcode
查看>>
find out the neighbouring max D_value by counting sort in stack
查看>>
ASP.NET MVC之分部视图和ChildAction(三)
查看>>
逆向工程 找不到文件
查看>>
OpenJ_Bailian——4115鸣人和佐助(带状态的A*)
查看>>
8.现代计算机的组成
查看>>
oracle系列--第三篇 Oracle的安装
查看>>
Javascript模块化编程(二):AMD规范
查看>>
吴裕雄--天生自然 物理学习与探索笔记:位移和时间
查看>>
[译]JavaScript:打破所有规则
查看>>
0909对操作系统的认识
查看>>
windows下的批处理bat文件和Linux下的shell文件的互相转换
查看>>
BZOJ【1606】购买干草
查看>>
MySQL 删除重复数据实例
查看>>
。。。
查看>>
【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)
查看>>
一分钟将你的WPF应用程序变身成炫彩动态Metro风格
查看>>
iOS 学习资料Blog 技术论坛等,不断添加中。。。。
查看>>