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文件,比 ...
随机推荐
- Checkbox Text 重影问题的解决的方法
Checkbox有个属性值 <CheckBox android:id="@+id/cb_reg_agree" style="@style/reg_checkbox_ ...
- leetcode笔记:Ugly Number II
一. 题目描写叙述 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prim ...
- ios 之 autoresizing小解
对于IOS的app开发人员来说,不会像Android开发人员一样为非常多的屏幕尺寸来做界面适配,因此硬编码的坐标也能工作良好,可是从设计模式上来说这不是好的做法. 并且也另一些问题,如iPhone5的 ...
- vue install 注册组件
1.myPlugin.js文件 let MyPlugin = {}; MyPlugin.install = function (Vue, options) { // 1. 添加全局方法或属性 Vue. ...
- Git 学习之--安装配置GitHub
楼主今天学习了一下Git的使用,而且Androdi studio 下加入了Git插件,成功提交项目到自己Github个人主页 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...
- Archlinux 下的 VMWare Workstation 维护笔记
印象中 Archlinux 下的 VMWare Workstation 总是出问题, 因此写这个帖子, 记录出问题时间/原因/解决方案. PS: 每次更新内核后可能需要重新编译 vmware 的内核模 ...
- 设计一个线程安全的单例(Singleton)模式
在设计单例模式的时候.尽管非常easy设计出符合单例模式原则的类类型,可是考虑到垃圾回收机制以及线程安全性.须要我们思考的很多其它.有些设计尽管能够勉强满足项目要求,可是在进行多线程设计的时候.不考虑 ...
- UbuntuServer12.04安装MongoDB,开机自启,服务,权限
获取最新版本 去http://www.mongodb.org/downloads找最新版的链接 wget http://fastdl.mongodb.org/linux/mongodb-linux-x ...
- c++引用返回值
引用作为函数的返回值时,函数的返回值能够理解为函数返回了一个变量(事实上,函数返回引用时,它返回的是一个指向返回值的隐式指针),因此,值为引用的函数能够用作赋值运算符的左操作数.另外,用引用返回一个函 ...
- linux SPI驱动——spi协议(一)
一:SPI简介以及应用 SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在 ...