【Programming Clip】位运算的应用

简介: 作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.用途 利用位运算,完成判断两个数字直接二进制的差异,数值交换,判断是否为2的次方,以及判断机器是SMALL_ENDIAN还是BIG_ENDIAN等。

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

1.用途

利用位运算,完成判断两个数字直接二进制的差异,数值交换,判断是否为2的次方,以及判断机器是SMALL_ENDIAN还是BIG_ENDIAN等。

2.描述语言

C++

3.原理

这个也没有什么原理,就是位运算,包括位移、与、或、异、取反或等。

4.代码

/*
 * =====================================================================================
 *        Version:  1.0
 *        Created:  01/09/2012 02:30:34 PM
 *       Revision:  none
 *       Compiler:  gcc/g++
 *
 *         Author:  gnuhpc (http://www.cnblogs.com/gnuhpc), gnuhpc#gmail.com
 *        Company:  CMBC
 *
 * =====================================================================================
 */
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <sstream>
#include <numeric>
#include <map>

using namespace std;

int bitDiff(int a,int b)
{
	int result=0;
	for (int i = a^b;i!=0;i>>=1)
	{
		result+=i&1;
	}
	return result;
}

void swap(int& a, int& b)
{
	if (a!=b)
	{
		a=a^b;
		b=a^b;
		a=a^b;
	}
}

bool judgepower2(int n)
{
	return (n&(n-1))==0;
}

int ByteOrder()
{
	//return 0 for BIG_ENDIAN -- the most significant byte in the smallest address
	//return 1 for SMALL_ENDIAN-- the least significant byte in the smallest address
	short int word = 0x0001;
	char *byte = (char*) &word;
	return (byte[0]?1:0);
}

int main()
{
	int m = 12; //00001100
	int n = 134;//10000110
	cout << bitDiff(m,n) <<endl;
	swap(m,n);
	cout <<"m=" <<m <<",n=" << n<<endl;

	cout.setf(ios::boolalpha);
	cout << judgepower2(64) <<endl;
	cout << judgepower2(6) <<endl;

	cout << ByteOrder() <<endl;
	return 0;
}

 

5.收获

1)在处理数值需要提高效率时可以从二进制的规律考虑,进行程序优化。

2)g++下,左移的规则是:向左移动,不管是否为符号位,丢弃最高位,低位补齐。左移一位相当于乘以2,由于int在C中是有符号的,最高位是符号位,0为正,1为负,所以左移可能产生溢出,即超过最大能表示的范围而变为了负值。

3)g++下,右移的规则是:向右移动,符号位正数补0负数补1(其实就是符号位不变),不存在溢出。

4)当位移的位数超过该数值类型的最大位数时,编译器会用位移位数去模类型的最大位数,然后按余数进行移位。

6.代码下载

http://gnuhpc.googlecode.com/svn/trunk/CPPExClip/bitopeartion.cpp


               作者:gnuhpc
               出处:http://www.cnblogs.com/gnuhpc/
               除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


分享到:

目录
相关文章
|
5月前
|
存储 算法 Python
算法专题1——动态规划 Dynamic Programming,DP
算法专题1——动态规划 Dynamic Programming,DP
65 0
|
人工智能 算法 自动驾驶
使用OpenCV实现Halcon算法(2)形状匹配开源项目,shape_based_matching
使用OpenCV实现Halcon算法(2)形状匹配开源项目,shape_based_matching
3156 0
使用OpenCV实现Halcon算法(2)形状匹配开源项目,shape_based_matching
|
9天前
|
机器人
动态规划Dynamic Programming
动态规划Dynamic Programming
11 0
|
9天前
|
机器学习/深度学习 存储 算法
算法·动态规划Dynamic Programming
算法·动态规划Dynamic Programming
10 0
|
4月前
|
算法 决策智能 Python
Python高级算法——线性规划(Linear Programming)
Python高级算法——线性规划(Linear Programming)
73 0
Python高级算法——线性规划(Linear Programming)
|
存储 算法 安全
动态规划(Dynamic Programming)
DP是解决多阶段决策过程中最优化问题的一种常用方法,它在算法中的重要性不言而喻,本文将帮助大家简单了解DP。
119 0
动态规划(Dynamic Programming)
|
Ubuntu Java Linux
开源线性规划求解器(Linear Programming solver)LP_Solve和CLP的PK
开源线性规划求解器(Linear Programming solver)LP_Solve和CLP的PK
1953 0
开源线性规划求解器(Linear Programming solver)LP_Solve和CLP的PK