zl程序教程

您现在的位置是:首页 >  其它

当前栏目

取n的某些位

某些
2023-09-11 14:21:07 时间

实例十一:取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;
};