CareerCup-5.1
5.1 CareerCup
2023-09-14 08:57:28 时间
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 bitset using namespace std; typedef int Data; void setBits(bitset 32 * N, bitset 32 * M, int i, int j) *N = *N^(*N (32-j+i-1) (32-j-1))^(*M i); void setBits2(bitset 32 * N, bitset 32 * M, int i, int j) bitset 32 t((~0 (j+1)|((1 i)-1))); *N = t *N|(*M i); int main() bitset 32 N((string)"10000000000"); bitset 32 M((string)"10101"); int i=2, j=6; setBits2( N, M, i, j); cout N endl; system("pause"); };
相关文章
- 10147. 「一本通 5.1 例 1」石子合并
- 10152. 「一本通 5.1 练习 3」矩阵取数游戏
- mplayer音乐播放器_potplayer 模拟5.1
- 【Swoole系列5.1】毫秒定时器
- 《软件绿色联盟应用体验标准5.1·安全标准》正式发布
- MySQL 5.1.48:稳定性和可靠性的保障(mysql5.1.48)
- MySQL 5.1:强大的数据库设计工具(mysql51)
- Bodhi Linux 5.1 一览: 略有不同的轻量化 Linux
- MySQL 5.1手册:详细学习数据库管理的必备指南(mysql5.1手册)
- MySQL 51安装指南全步骤指引(5.1mysql安装步骤)
- ASP.NET.4.5.1+MVC5.0设置系统角色与权限(一)