1. 一个数的从右起第p1位和第p2位swap n位

  1. unsigned int swapBits(unsigned int x, unsigned int p1, unsigned int p2, unsigned int n) {
  2. unsigned int set1 = (x >> p1) & (( << n) - );
  3. unsigned int set2 = (x >> p2) & (( << n) - );
  4. unsigned int xored = set1 ^ set2; //same is 0, different is 1
  5. xored = (xored << p1) | (xored << p2);
  6. return x ^ xored; //anybit ^ 0 = anybit, anybit ^ 1 = !anybit, so same no need to change, different need to be changed
  7. }
  8.  
  9. int main()
  10. {
  11. cout << swapBits(, , , ) << endl;
  12. return ;
  13. }

2. Add two integer

  1. int Add(int x, int y) {
  2. while (y) {
  3. int carry = x & y;
  4. x = x ^ y;
  5. y = carry << ;
  6. // cout << carry << " " << x << " " << y << endl;
  7. }
  8. return x;
  9. }

3. min函数

  1. int minnum(int x, int y) {
  2. return y + ((x - y) & ((x - y) >> (sizeof(int) * - )));
  3. }

also can use division

4. count bit set in an integer

  1. int countsetbits(int x) {
  2. int ans = ;
  3. while(x) {
  4. x &= (x-);
  5. ans++;
  6. }
  7. return ans;
  8. }

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. Checkbox Text 重影问题的解决的方法

    Checkbox有个属性值 <CheckBox android:id="@+id/cb_reg_agree" style="@style/reg_checkbox_ ...

  2. leetcode笔记:Ugly Number II

    一. 题目描写叙述 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prim ...

  3. ios 之 autoresizing小解

    对于IOS的app开发人员来说,不会像Android开发人员一样为非常多的屏幕尺寸来做界面适配,因此硬编码的坐标也能工作良好,可是从设计模式上来说这不是好的做法. 并且也另一些问题,如iPhone5的 ...

  4. vue install 注册组件

    1.myPlugin.js文件 let MyPlugin = {}; MyPlugin.install = function (Vue, options) { // 1. 添加全局方法或属性 Vue. ...

  5. Git 学习之--安装配置GitHub

    楼主今天学习了一下Git的使用,而且Androdi studio 下加入了Git插件,成功提交项目到自己Github个人主页 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...

  6. Archlinux 下的 VMWare Workstation 维护笔记

    印象中 Archlinux 下的 VMWare Workstation 总是出问题, 因此写这个帖子, 记录出问题时间/原因/解决方案. PS: 每次更新内核后可能需要重新编译 vmware 的内核模 ...

  7. 设计一个线程安全的单例(Singleton)模式

    在设计单例模式的时候.尽管非常easy设计出符合单例模式原则的类类型,可是考虑到垃圾回收机制以及线程安全性.须要我们思考的很多其它.有些设计尽管能够勉强满足项目要求,可是在进行多线程设计的时候.不考虑 ...

  8. UbuntuServer12.04安装MongoDB,开机自启,服务,权限

    获取最新版本 去http://www.mongodb.org/downloads找最新版的链接 wget http://fastdl.mongodb.org/linux/mongodb-linux-x ...

  9. c++引用返回值

    引用作为函数的返回值时,函数的返回值能够理解为函数返回了一个变量(事实上,函数返回引用时,它返回的是一个指向返回值的隐式指针),因此,值为引用的函数能够用作赋值运算符的左操作数.另外,用引用返回一个函 ...

  10. linux SPI驱动——spi协议(一)

    一:SPI简介以及应用 SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在 ...