Algorithm: bit manipulation
1. 一个数的从右起第p1位和第p2位swap n位
unsigned int swapBits(unsigned int x, unsigned int p1, unsigned int p2, unsigned int n) {
unsigned int set1 = (x >> p1) & (( << n) - );
unsigned int set2 = (x >> p2) & (( << n) - );
unsigned int xored = set1 ^ set2; //same is 0, different is 1
xored = (xored << p1) | (xored << p2);
return x ^ xored; //anybit ^ 0 = anybit, anybit ^ 1 = !anybit, so same no need to change, different need to be changed
}
int main()
{
cout << swapBits(, , , ) << endl;
return ;
}
2. Add two integer
int Add(int x, int y) {
while (y) {
int carry = x & y;
x = x ^ y;
y = carry << ;
// cout << carry << " " << x << " " << y << endl;
}
return x;
}
3. min函数
int minnum(int x, int y) {
return y + ((x - y) & ((x - y) >> (sizeof(int) * - )));
}
also can use division
4. count bit set in an integer
int countsetbits(int x) {
int ans = ;
while(x) {
x &= (x-);
ans++;
}
return ans;
}
Algorithm: bit manipulation的更多相关文章
- VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟
C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...
- Computer Vision Algorithm Implementations
Participate in Reproducible Research General Image Processing OpenCV (C/C++ code, BSD lic) Image man ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- HDU2441 ACM(Array Complicated Manipulation)
ACM(Array Complicated Manipulation) Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32 ...
- programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation
编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...
- bit manipulation
WIKI Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorte ...
- boost string algorithm
The Boost.StringAlgorithms library provides many free-standing functions for string manipulation. 1. ...
- 挑子学习笔记:两步聚类算法(TwoStep Cluster Algorithm)——改进的BIRCH算法
转载请标明出处:http://www.cnblogs.com/tiaozistudy/p/twostep_cluster_algorithm.html 两步聚类算法是在SPSS Modeler中使用的 ...
- PE Checksum Algorithm的较简实现
这篇BLOG是我很早以前写的,因为现在搬移到CNBLOGS了,经过整理后重新发出来. 工作之前的几年一直都在搞计算机安全/病毒相关的东西(纯学习,不作恶),其中PE文件格式是必须知识.有些PE文件,比 ...
随机推荐
- SVN merge 三种方式
1.Merge a range of revisions 2.Reintegrate a branch 3.Merge two different trees ———————————————————— ...
- 【京东个人中心】——Nodejs/Ajax/HTML5/Mysql爬坑之功能与数据分析
一.引言 在学习了Nodejs和HTML5之后,发现了Nodejs的使用很方便,和php是完全不同的另一种后台语言.我也明白了,在一个项目里,是不可能同时存在Apach服务器(php)和Web服务器( ...
- 使用REST-Assured对API接口进行自动化测试
转载:http://blog.csdn.net/u012050416/article/details/50674612 准备 目标 开始编码 总结 说明:本文只是一个getStart示例,关键在于 ...
- /etc/shadow 密码加密方法
[root@mysql-master ~]# cat /etc/shadowroot:$6$spzQDWctb8Lmju0o$KoUz5Qwv1tWyVYfd5cuBw.TQVIaCvCX8ixGG9 ...
- url 传中文
if (null == keyword || keyword.equals("关键字")) keyword = ""; if(keyword.length()& ...
- memcpy( )的使用以及迭代器的使用
memcpy() -- 拷贝内存内容 相关函数: bcopy(), memccpy(), memmove(), strcpy(), strncpy() 表头文件: #include <strin ...
- JSConsole调试
http://jsconsole.com/ https://github.com/remy/jsconsole
- mysql主从只同步部分库或表
同步部分数据有两个思路,1.master只发送需要的:2.slave只接收想要的. master端: binlog-do-db 二进制日志记录的数据库(多数据库用逗号,隔开)binlog-i ...
- Android Studio/IntelliJ IDEA使用手记
使用第三方jar包 1.将jar包放入项目里的libs文件夹中: 2.在project选中该jar包,点击右键选择:"Add as library": 1. 代码中中文显示乱码 ...
- svn自动部署
版本库目录hooks下创建post-commit.bat TortoiseProc.exe /command:update /path:"E:\web_server\sial\" ...