一些用法一一利用Mask记录一些开关选项

        在一些项目使用中,我们经常需要确认项目中某些选择项是否勾选/有效,有时会采用数组的形式进行记录,但是读取的时候需要对数组进行遍历,记录前后状态,这里,我们可以采用Mask的机制来完成,此种用法主要用到了十六进制计数方法,便于按位求非(剔除)及按位求与(增加)操作。

下面根据示例说明:

#define  MAX_SLOT 4

static unsigned long	gulMask[]=
{
	0x00000001,
	0x00000002,
	0x00000004,
	0x00000008,
	0x00000010,
	0x00000020,
	0x00000040,
	0x00000080,
	0x00000100,
	0x00000200,
	0x00000400,
	0x00000800,
	0x00001000,
	0x00002000,
	0x00004000,
	0x00008000,
	0x00010000,
	0x00020000,
	0x00040000,
	0x00080000,
	0x00100000,
	0x00200000,
	0x00400000,
	0x00800000,
	0x01000000,
	0x02000000,
	0x04000000,
	0x08000000,
	0x10000000,
	0x20000000,
	0x40000000,
	0x80000000,
};
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include "MaskTest.h"
using namespace std;

int g_bEnable[MAX_SLOT] = {false};

int main()
{
	char szDir[MAX_PATH]="";
	int  iMask = 0;
	GetCurrentDirectory(sizeof(szDir),szDir);
	char szIniPath[MAX_PATH]="";
	sprintf(szIniPath,"%s\\Config.ini",szDir);
	iMask = GetPrivateProfileInt("SlotEnable","SlotMask",1,szIniPath);

	//根据配置文件中的SlotMask值来确定哪些Slot是Enable的
	for (int index=0;index<MAX_SLOT;index++)
	{
		if ((iMask & gulMask[index]) == gulMask[index])
		{
			g_bEnable[index] = true;
			cout<<"Slot "<<index<<"Enable"<<endl;
		}
	}

	int i = 0;
	iMask = 0;
	//改变Slot的Enable值,然后重新存入到配置文件中去
	for (int index=0;index<MAX_SLOT;index++)
	{
		cout<<"Slot: "<<index<<"Enable or not?"<<endl;
		cin>>i;
		g_bEnable[index] = i;
		if (g_bEnable[index])
		{
			iMask += gulMask[index];
		}
	}

	char czMask[10]="";
	itoa(iMask,czMask,10);
	WritePrivateProfileString("SlotEnable","SlotMask",czMask,szIniPath);

	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值