一些有关C++语言中的bit操作

AuthorJeff    2005-11-30

关键字:C/C++  bit  位操作

 

以下大多从网上和书中来,有些则加入了个人的见解~~

1)  函数实现交换两个int型变量的值,要求不定义任何中间变量。

C / C++ 版:

void Swap(int *a, int *b)

{

*a^=*b;

   *b^=*a;

   *a^=*b;

}

C++ 版:

void Swap(int &a, int &b)

{

a^=b;

   b^=a;

   a^=b;

}

分析一下:

(a ^ b = s)       (b ^ s = a)

0 ^ 1 = 1        1 ^ 1 = 0

1 ^ 0 = 1        0 ^ 1 = 1

1 ^ 1 = 0        1 ^ 0 = 1

0 ^ 0 = 0        0 ^ 0 = 0

归结一下:1. 两个数相异或,相同的位清0,不同的位置1 (所以才有了 XOR eax, eax ;eax清零)

          2. s = (a ^ b); (b ^ s) == a ? Yes. 异或加密解密的基础.

 

2) 摘自《高效程序的奥秘》一书。英文书名<Hackers Delight>,译为《黑客的窃喜》

   获得后缀0bit的个数。(从低位开始的连续的0bit,间接地求最低位的1bit的位置)

int ntz(unsigned x)  {         // Number of trailing zeros.

    int n;

if (x == 0) return(32);

n = 1;

if ((x & 0x0000FFFF) == 0) {n = n +16; x = x >>16;}

if ((x & 0x000000FF) == 0) {n = n + 8; x = x >> 8;}

        if ((x & 0x0000000F) == 0) {n = n + 4; x = x >> 4;}

        if ((x & 0x00000003) == 0) {n = n + 2; x = x >> 2;}

        return n - (x & 1);

}

 

3) 除了最低位的bit1,其余位全清0. <Hackers Delight>一书。

   n = n & (-n)   < - - - - > n = n & (~n + 1)

分析一下:

假设n(2) = XX100。其中每个X代表bit值并不都是相同,0的个数0¯X表示该位取反。

X & ¯X = 0, 1 & 0 = 0

    ~n(2) = ¯X¯X011

      ~n + 1 = ¯X¯X100

         n & (~n + 1) = 00100

 

4) 清最低位的bit1其他不变

   n = n & (n – 1);

    n – 1 = XX011

      n & (n - 1) = XX000

5) 获得bit1的个数。(统计)

   我们常用的方法:

int Get1BitCount(unsigned int x)

{

   int n = 0;

while (x > 0) {

    if (x & 0x1) n++;

    x >>= 1;

}

return n;

}

另一种更好的办法:

int Get1BitCount(unsigned int x)

{

    int  n = 0;

    while (x > 0) {

n++;

x = x & (x - 1);

     }

     return n;

}

6) unsigned int型数一共有多少bit?(网上看到有人问)

 int  nCount = Get1BitCount((unsigned int)~0);

 求别的数据类型的位数,要重载Get1BitCount(或者直接用temlate来实现)

 不过觉得像上面这样求法,太浪费了,不是吗?

 int GetTotalBit(void)

{

    unsigned int x = ~0;

    int  n = 0;

    while (x > 0) {

n += 8;

x >>= 8;

     }

     return n;

}

7)  清除低位到高位的nbit1

    while (--n > 0) {

        x = x & (x - 1);

}

8)  (以后有再慢慢更新)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
这是一个用于C++ MFC开发的Bitmap图片操作类,在文件叫CBitmapEx,可用于放大,缩小,翻转,过渡和其他有用的功能,有兴趣的朋友可以下载看看。 部分public method: // // void Create(long width, long height); // void Create(CBitmapEx& bitmapEx); // void Load(LPTSTR lpszBitmapFile); // void Save(LPTSTR lpszBitmapFile); // void Scale(long horizontalPercent=100, long verticalPercent=100); // void Rotate(long degrees=0, _PIXEL bgColor=_RGB(0,0,0)); // void FlipHorizontal(); // void FlipVertical(); // void MirrorLeft(); // void MirrorRight(); // void MirrorTop(); // void MirrorBottom(); // void Clear(_PIXEL clearColor=_RGB(0,0,0)); // void Negative(); // void Grayscale(); // void Sepia(long depth=34); // void Emboss(); // void Engrave(); // void Pixelize(long size=4); // void Draw(HDC hDC); // void Draw(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY); // void Draw(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, long alpha); // void Draw(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight); // void Draw(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, // long srcX, long srcY, long srcWidth, long srcHeight, long alpha); // void DrawTransparent(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, long alpha, // _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, // _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, // long alpha, _PIXEL transparentColor=_RGB(0,0,0)); // LPBI
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值