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文件,比 ...
随机推荐
- MFC中 CString类型用fprintf 函数写到文件中乱码的解决办法
在上一篇中记录了用fprintf函数写内容到文件中的方法,但是发现了问题:产生的文件字符串有乱码现象. 解决办法:用_ftprintf函数 另外,据说: unicode的话要用fwprintf ...
- appium 'WebDriver' object has no attribute 'keyevent'
这个问题是我自己犯二了,开头应该是from appium import webdriver,写成了from selenium import webdriver,也可以运行,就是不能使用appium中独 ...
- MapWindowPoints
中文名 MapWindowPoints Windows CE 1.0及以上版本 头文件 winuser.h 库文件 user32.lib MapWindowPoints函数把相对于一个窗口的坐标空间的 ...
- dede二级导航与二级栏目 ----内容介绍二级导航
{dede:channelartlist typeid='top'}//如果只需要拿一列,则需要使用row='1'这个属性否则会根据子频道的数目循环输出 <a href="{dede: ...
- HP Vertica Analytics Platform 评測
1.vertica概念 面向数据分析的数据仓库系统解决方式 2.vertica关键特性 Ø 标准的SQL接口:能够利用已有的BI.ETL.Hadoop/MapReduce和OLTP环境 Ø 高可用 ...
- DWR3.0(Direct Web Remoting)实践
“DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and c ...
- HDFS源码分析心跳汇报之周期性心跳
HDFS源码分析心跳汇报之周期性心跳,近期推出!
- eval(function(p,a,c,k,e,d){e=function(c)加解密
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HTML经典标签用法
1.marquee属性的使用说明 <marquee> ... </marquee>移动属性的设置 ,这种移动不仅仅局限于文字,也可以应用于图片,表格等等 鼠标属性 onMo ...
- implode 函数 把数组拼接成字符串
$array( '0'=>1, '1'=>5, '2'=>5 ); $str=imploade(',',$array); echo str;//输出1,5,3