如何让一个变量存储多个不同的状态

#include <stdio.h>
#include <assert.h>

typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;

BYTE GetBitCount( DWORD Number )
{
	BYTE Count = 0;
	while( Number )
	{
		if( ( Number & 1 ) > 0 )
		{
			++Count;
		}
		Number >>= 1;
	}
	return Count;
}

BYTE GetBitValue( const BYTE Index, const BYTE Array )
{
	if( Index > 7 )
	{
		assert( false );
		return ~0;
	}
	return Array >> ( 6 - Index ) & 1;
}

bool SetBitValue( const BYTE Index, const bool BitValue, BYTE& Array )
{
	if( Index > 7 )
	{
		assert( false );
		return false;
	}
	const BYTE TBitValue = 1 << ( 6 - Index );
	//1[0]1
	//	|
	//0[1]0
	if( BitValue )
	{
		Array |= TBitValue;
	}
	//1[1]1
	//	&
	//0[1]0
	//	~
	//1[0]1
	else
	{
		Array &= ~TBitValue;
	}
	return true;
}

BYTE GetBYTEValue( const BYTE Index, const WORD Array )
{
	if( Index > 2 )
	{
		return ~0;
	}
	return ( ( BYTE* )&Array)[ Index ];
}

bool SetBYTEValue( const BYTE Index, const BYTE& Value, WORD& Array )
{
	if( Index > 2 )
	{
		return false;
	}
	( ( BYTE* )&Array )[ Index ] = Value;
	return true;
}

BYTE GetBYTEValue( const BYTE Index, const DWORD Array )
{
	if( Index > 4 )
	{
		return ~0;
	}
	return ( ( BYTE* )&Array)[ Index ];
}

bool SetBYTEValue( const BYTE Index, const BYTE& Value, DWORD& Array )
{
	if( Index > 4 )
	{
		return false;
	}
	( ( BYTE* )&Array )[ Index ] = Value;
	return true;
}

WORD GetWORDValue( const BYTE Index, const DWORD Array )
{
	if( Index > 2 )
	{
		return ~0;
	}
	return ( ( WORD* )&Array)[ Index ];
}

bool SetWORDValue( const BYTE Index, const WORD& Value, DWORD& Array )
{
	if( Index > 2 )
	{
		return false;
	}
	( ( WORD* )&Array )[ Index ] = Value;
	return true;
}

int main()
{
	//1.让一个BYTE变量拥有几个不同的两种状态而不需要使用数组而且比数组更节约空间
	//我目前实现的是一个位存一个数据则只能表达0,1这两种状态,大家可以扩展使用两个位
	//来存储数据则可以表达00-11的数据可以扩展到0,1,2,3四种状态但空间会缩小一半一个BYTE占8bit/2bit
	//只能存4个可以表达最多4种状态的元素,不扩展的话原来一个BYTE 8bit/1bit则可以存到8个元素
	//这种方式非常适合数据库,一个字段想要表达几个不同的两种状态,而不用新建字段
	//或者某中条件中有限制只能用一个变量表达几个不同的两种状态
	//或者数据只需要几个位就能完事的完全没有用到1字节8bit这么多的数据因为最小定义的基本类型都要使用到1个字节
	//我只写了BYTE的最多可以存8个不同两种状态,再多可以改写成用WORD最多存16 DWORD最多存32
	//再多就没必要用这种方式了,因为相对就比较复杂了
	BYTE Array = 0;
	SetBitValue( 0, true, Array );
	SetBitValue( 1, false, Array );
	SetBitValue( 2, false, Array );
	SetBitValue( 3, true, Array );
	for( BYTE i = 0; i < 4; ++i )
	{
		printf( "%d\n", GetBitValue( i, Array ) );
	}
	printf( "\n" );
	//2.让一个WORD保存两个BYTE变量
	WORD Array2 = 0;
	SetBYTEValue( 0, 1, Array2 );
	SetBYTEValue( 1, 23, Array2 );

	for( BYTE i = 0; i < 2; ++i )
	{
		printf( "%d\n", GetBYTEValue( i, Array2 ) );
	}
	printf( "\n" );
	//3.让一个DWORD保存四个BYTE变量
	//这种方式非常适合存储颜色
	//如:
	DWORD Array3 = 0;
	SetBYTEValue( 0, 0xFF, Array3 );//Alpha
	SetBYTEValue( 1, 0x12, Array3 );//R
	SetBYTEValue( 2, 0x20, Array3 );//G
	SetBYTEValue( 3, 0xA0, Array3 );//B
	for( BYTE i = 0; i < 4; ++i )
	{
		printf( "%d\n", GetBYTEValue( i, Array3 ) );
	}
	printf( "\n" );
	//4.让一个DWORD保存两个WORD变量
	DWORD Array4 = 0;
	SetWORDValue( 0, 0xFFFF, Array4 );
	SetWORDValue( 1, 0xF0, Array4 );
	for( BYTE i = 0; i < 2; ++i )
	{
		printf( "%d\n", GetWORDValue( i, Array4 ) );
	}
	printf( "\n" );
	//5.附加算法,求二进制位为1的数量
	printf( "%d\n", GetBitCount( 0xFFFFFFFF ) );
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中,可以使用`Vuex`来实现多组件共用一个变量。`Vuex`是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。 具体实现步骤如下: 1. 安装`Vuex` 使用 npm 安装: ``` npm install vuex --save ``` 2. 创建 store 在项目根目录下创建`store`文件夹,在该文件夹下创建`index.js`文件,编写以下代码: ```javascript import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ } }, actions: { increment (context) { context.commit('increment') } } }) ``` 上述代码中,我们定义了一个状态`count`和一个`increment`的`mutation`方法,用来修改`count`的值。 3. 在组件中使用 在需要使用`count`的组件中,可以通过以下方式来使用: ```javascript <template> <div> <p>{{ count }}</p> <button @click="increment">加1</button> </div> </template> <script> import { mapState, mapActions } from 'vuex' export default { computed: { ...mapState(['count']) }, methods: { ...mapActions(['increment']) } } </script> ``` 上述代码中,我们使用了`mapState`和`mapActions`来映射`state`和`actions`中的方法和变量。这样,在模板中就可以直接使用`count`变量和`increment`方法了。 这样,多个组件就可以共用一个状态`count`了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值