参考于http://www.sjsjw.com/kf_code/article/86_31843_24280.asp
#include<iostream>
using namespace std;
class BitOperation{
public:
int getMaxInt(){ //获得Int型最大值
return ((unsigned int)-1)/2;
//return (1<<31)-1;
//return ~(1<<31);
}//getMaxInt
int getMinInt(){ //获得Int型最小值
return 1<<31;
}//getMinInt
long getMaxLong(){ //获得Long型最大值
return ((unsigned long)-1)/2;//2147483648
//Java return ((long)1<<127)-1; 922337203685477
}//getMaxLong
int mulTwo(int n){ //n*2
return n<<1;
}//mulTwo
int divTwo(int n){ //n/2
return n>>1;
}//divTwo
int mulTwoPower(int n,int m){ //n*2^m
return n<<m;
}//mulTwoPower
int divTwoPower(int n,int m){ //n/2^m
return n>>m;
}//divTwoPower
bool isOddNumber(int n){ //判断一个数的奇偶性
return (n&1)==1;
}//isOddNumber
void swap(int *a,int *b){ //交换两个数
// a^=b;b^=a;a^=b;
(*a)^=(*b)^=(*a)^=(*b);
}//swap
int abs(int n){ //取绝对值
return (n^(n>>31))-(n>>31);
//return n>0?n:-n;
}//abs
int max(int a,int b){ //获得两个数中的最大值
return a^((a^b) & -(a<b));
//return b&((a-b)>>31) | a&(~(a-b)>>31);
//return (a+b+abs(a-b))/2;
//return a>b?a:b;
}//max
int min(int a,int b){ //获得两个数中的最小值
//return a>b?b:a;
//return (a+b-abs(a-b))/2;
//return a&((a-b)>>31) | b&(~(a-b)>>31);
return a^((a^b) & -(a>b));
}//min
int isSameSign(int x,int y){ //判断两个数字是否同号,同号返回1,异号返回0
return (x^y)>-1;
}//isSameSign
int pow2(int n){ //2^n
return 2<<(n-1);
}//pow2
int isFactorialofTwo(int n){ //判断一个数是不是2的幂
return (n&(n-1))==0;
}//isFactorialofTwo
int quyu(int m,int n){ //对n^2取余
return m&(n-1);
}//quyu
int getAverage(int x,int y){ //求两个数的平方数
return ((x^y)>>1)+(x&y);
}//getAverage
int getBit(int n,int m){ //低位到高位,取n的第m位
return (n>>(m-1))&1;
}//getBit
int setBitToOne(int n,int m){ //低位到高位,将n的第m位置置1
return n|(1<<(m-1));
}//setBitToOne
int setBitToZero(int n,int m){//低位到高位,将n的第m位置0
return n&(0<<(m-1));
}//setBitToZero
int addOne(int n){ //n+1
return -~n;
}//addOne
int minusOne(int n){ //n-1
return ~-n;
}//minusOne
int OppositeNumber(int n){ //取相反数
return ~n+1;
}//OppositeNumber
int sign(int n){ //正数为1,零为0,负数为-1
return !!n-(((unsigned)n>>31)<<1);
}//sign
};
int main(){
BitOperation BO;
cout<<"BO.getMaxInt() "<<BO.getMaxInt()<<endl;
cout<<"BO.getMinInt() "<<BO.getMinInt()<<endl;
cout<<"BO.getMaxLong() "<<BO.getMaxLong()<<endl;
cout<<"BO.mulTwo(2) "<<BO.mulTwo(2)<<endl;
cout<<"BO.divTwo(2) "<<BO.divTwo(2)<<endl;
cout<<"BO.mulTwoPower(1024,2) "<<BO.mulTwoPower(1024,2)<<endl;
cout<<"BO.divTwoPower(1024,2) "<<BO.divTwoPower(1024,2)<<endl;
cout<<"BO.isOddNumber(3) "<<BO.isOddNumber(3)<<endl;
cout<<"BO.isOddNumber(4) "<<BO.isOddNumber(4)<<endl;
cout<<"BO.swap(&a,&b)"<<endl;
int a=2,b=3;
cout<<a<<" "<<b<<endl;
BO.swap(&a,&b);
cout<<a<<" "<<b<<endl;
cout<<"BO.abs(-1) "<<BO.abs(-1)<<endl;
cout<<"BO.abs(1) "<<BO.abs(1)<<endl;
cout<<"BO.max(100,1) "<<BO.max(100,1)<<endl;
cout<<"BO.min(100,1) "<<BO.min(100,1)<<endl;
cout<<"BO.isSameSign(-1,-1) "<<BO.isSameSign(-1,-1)<<endl;
cout<<"BO.isSameSign(-1,1) "<<BO.isSameSign(-1,1)<<endl;
cout<<"BO.pow2(2) "<<BO.pow2(2)<<endl;
cout<<"BO.isFactorialofTwo(1024) "<<BO.isFactorialofTwo(1024)<<endl;
cout<<"BO.isFactorialofTwo(1023) "<<BO.isFactorialofTwo(1023)<<endl;
cout<<"BO.quyu(20,10) "<<BO.quyu(20,10)<<endl;
cout<<"BO.getAverage(10,20) "<<BO.getAverage(10,20)<<endl;
//0000 0000 0000 0000 0000 0000 0000 0001 (1)
//0000 0000 0000 0000 0000 0000 0000 0011 (3)
cout<<"BO.setBitToOne(1,2) "<<BO.setBitToOne(1,2)<<endl;
//0000 0000 0000 0000 0000 0000 0000 0000 (0)
cout<<"BO.setBitToZero(1,1) "<<BO.setBitToZero(3,1)<<endl;
cout<<"BO.addOne(1) "<<BO.addOne(1)<<endl;
cout<<"BO.minusOne(1) "<<BO.minusOne(1)<<endl;
cout<<"BO.OppositeNumber(1) "<<BO.OppositeNumber(1)<<endl;
cout<<"BO.OppositeNumber(-1) "<<BO.OppositeNumber(-1)<<endl;
cout<<"BO.sign(200) "<<BO.sign(200)<<endl;
cout<<"BO.sign(0) "<<BO.sign(0)<<endl;
cout<<"BO.sign(-200) "<<BO.sign(-200)<<endl;
return 0;
}