【C++】位操作(3)-获取某位的值

获取某位的值,从低位到高位(右到左)

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;

int ok(int n,int _Mask)  
{  
	_Mask = _Mask -1;
	int a=1;
	a<<=_Mask;
	n&=a;
	if(n == a)
	{
		return 1;
	}
	return 0;
}  

int _tmain(int argc, _TCHAR* argv[])	
{
	cout << ok(3,3);
	cout << ok(3,2);
	cout << ok(3,1);
	cout<<endl;
	system("pause");
	return 0;
}


原文地址:https://www.cnblogs.com/byfei/p/14104260.html