取n的某些位

实例十一:取n的某些位

方法:result=(n>>4)&(~(~0<<4))

取出某数n的低4位。

数值0         0000 0000
~0           1111 1111
~0<<4          1111 0000
~(~0<<4)      0000 1111


int _tmain(int argc, _TCHAR* argv[])
{
  int n,m,nResult = 0;

  cout << "请输入原始的值:";
  cin >> n;

  m = n >> 4;

  nResult = m &(~(~0<<4));

  cout << endl << nResult;

  system("pause");
  return 0;
};

原文地址:https://www.cnblogs.com/gd-luojialin/p/6635620.html