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. HDU1565方格取数

    典型的状态压缩DP问题.第i行的取法只受到第i-1行的影响.首先每一行的取法要相容(不能有两个相邻),然后相邻行之间也要相容.将每一个格子看做两种状态,1表示取,0表示不取.这样每一行就是一个01串, ...

  2. SilverLight-DataBinding-DataTemplates: 三、数据绑定 DataTemplates模板的使用(求助,没有到达实例效果,求高人指点迷津)

    ylbtech-SilverLight-DataBinding-DataTemplates: 三.数据绑定 DataTemplates模板的使用 1.A, Data Templates Intro(数 ...

  3. python+tesseract验证码识别的一点小心得

    由于公司需要,最近开始学习验证码的识别 我选用的是tesseract-ocr进行识别,据说以前是惠普公司开发的排名前三的,现在开源了.到目前为止已经出到3.0.2了 当然了,前期我们还是需要对验证码进 ...

  4. 2017.2.20 activiti实战--第二章--搭建Activiti开发环境及简单示例(二)简单示例

    学习资料:<Activiti实战> 第一章 搭建Activiti开发环境及简单示例 2.5 简单流程图及其执行过程 (1)leave.bpmn 后缀名必须是bpmn.安装了activiti ...

  5. 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(二) controller

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第二十一章-授予身份与切换身份(二) 1.回顾 ...

  6. Wireshark网络分析实战笔记(三)基本信息统计工具的使用方法

    Capture File Properties:获取网络中数据包的整体信息 用法:Statistics菜单条下Capture File Properties选项 Protocol Hierarchy: ...

  7. 关于meta标签的name="viewport" 概述

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scal ...

  8. VMware Workstation 永久许可证密钥

    VMware是功能最强大的虚拟机软件,用户可在虚拟机同时运行各种操作系统,进行开发.测试.演示和部署软件,虚拟机中复制服务器.台式机和平板环境,每个虚拟机可分配多个处理器核心.主内存和显存. VMwa ...

  9. cacti 主机/网络设备流量监控 图解

    1.在配置中找到设备 console —>  Device 2.初次添加 cacti 监控主机的时候是没有任何设备的,所以要选择add 添加你要监控的主机 \

  10. iOS对象(字典或数组)转化为JSon字符串

    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; [dictionary setValue:@"he ...