有符号数和无符号数的移位操作

3152阅读 0评论2012-07-25 时间看来
分类:C/C++

点击(此处)折叠或打开

  1. /*C provides six operators (&, |, ^, <<, >>,~)for bit manipulation;
  2.  * these may only be applied to integral operands, that is, char, short, int,
  3.  * and long,whether signed or unsigned.
  4.  */

  5. #include <iostream>
  6. using namespace std;

  7. int main() {
  8.     cout << "!!!Hello World!!!" << endl; // prints !!!Hello

  9.     /*二进制输出*/
  10.     cout.setf(ios::hex,ios::basefield);

  11.     int i = 0x80000000;
  12.     cout << "i = " << i <<endl;
  13.     i = i >> 1;
  14.     cout << "after move i = " << i << endl;

  15.     unsigned int ui = 0x80000000;
  16.     cout << "ui = " << ui <<endl;
  17.     ui = ui >> 1;
  18.     cout << "after move ui = " << ui << endl;

  19.     return 0;
  20. }
  21. //right shift
  22. //!!!Hello
  23. //i = 80000000
  24. //after move i = c0000000
  25. //ui = 80000000
  26. //after move ui = 40000000*/

  27. //left shift
  28. //!!!Hello
  29. //i = 80000000
  30. //after move i = 0
  31. //ui = 80000000
  32. //after move ui = 0

上一篇:网络变压器&网络滤波器
下一篇:求两个整型数组的异集,即A+B-(A与B的交集)