zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【C++】位应用(2)-设置某位的值

C++应用 设置
2023-09-14 09:06:31 时间

低到高(左到右)

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

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

void ok(int n,int _Mask)  
{  
	_Mask = _Mask -1;
	int a=1;
	a<<=_Mask;
	n|=a;
	cout<<n<<endl;
}  

//5 = 101
//7 = 111		
//8 = 1000
//9 = 1001
int _tmain(int argc, _TCHAR* argv[])	
{
	ok(5,2);  
	ok(8,1);  
	system("pause");
	return 0;
}