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的更多相关文章

  1. 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 ...

  2. Computer Vision Algorithm Implementations

    Participate in Reproducible Research General Image Processing OpenCV (C/C++ code, BSD lic) Image man ...

  3. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  4. HDU2441 ACM(Array Complicated Manipulation)

    ACM(Array Complicated Manipulation) Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32 ...

  5. programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation

    编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...

  6. bit manipulation

    WIKI Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorte ...

  7. boost string algorithm

    The Boost.StringAlgorithms library provides many free-standing functions for string manipulation. 1. ...

  8. 挑子学习笔记:两步聚类算法(TwoStep Cluster Algorithm)——改进的BIRCH算法

    转载请标明出处:http://www.cnblogs.com/tiaozistudy/p/twostep_cluster_algorithm.html 两步聚类算法是在SPSS Modeler中使用的 ...

  9. PE Checksum Algorithm的较简实现

    这篇BLOG是我很早以前写的,因为现在搬移到CNBLOGS了,经过整理后重新发出来. 工作之前的几年一直都在搞计算机安全/病毒相关的东西(纯学习,不作恶),其中PE文件格式是必须知识.有些PE文件,比 ...

随机推荐

  1. eclipse高速查找一个变量、方法或者类被引用的地方

    近期不停debug,拿到一个变量之后总是要先概览一下才好下手,之前一直用Ctrl+F来做,太麻烦. 今天查了下eclipse使用,发现有快捷键,用法: 先双击要查看的变量.方法或者类,使之被选中,然后 ...

  2. 【Python】python3中urllib爬虫开发

    以下是三种方法 ①First Method 最简单的方法 ②添加data,http header 使用Request对象 ③CookieJar import urllib.request from h ...

  3. GridView 获取列字段的几种途径

    GridView是ASP.NET中功能强大的数据显示控件,它的RowDataBound事件为我们提供了方便的控制行.列数据的途径. 要获取当前行的某个数据列,我在实践中总结有如下几种方法: 1. Ce ...

  4. css:颜色名和十六进制数值

    http://www.w3school.com.cn/cssref/css_colornames.asp

  5. SpringBoot学习之常用注解

    @SpringBootAppliaction:通常注解写在SpringBoot启动类中,主要包括三个作用: 1.@Configuration表示将该类作用springboot配置文件类. 2.@Ena ...

  6. 监听iOS检测屏幕旋转状态,不需开启屏幕旋转

    -(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...

  7. EMI-CLK信号串电阻并电容

    一般DMIC的CLK都会EMI超标,所以看到的案子这个DMIC CLK信号都会源端串接电阻和并电容 1,串电阻是为了信号的完整性,考虑到匹配的,一般说来这个电阻不是固定的,要随实际的PCB的走线的阻抗 ...

  8. ros学习网站

    ROS机器人操作系统入门-中国大学MOOC      https://www.bilibili.com/video/av24585414/?p=39 http://i.youku.com/i/UNDA ...

  9. Hibernate学习二----------hibernate简介

    © 版权声明:本文为博主原创文章,转载请注明出处 1.hibernate.cfg.xml常用配置 - hibernate.show_sql:是否把Hibernate运行时的SQL语句输出到控制台,编码 ...

  10. 16 nginx实现负载均衡

    一:nginx实现负载均衡-----------------原理-------------------------- (1) 反向代理后端如果有多台服务器,自然可形成负载均衡,但proxy_pass如 ...