#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;
}
如何让一个变量存储多个不同的状态
最新推荐文章于 2022-06-12 17:09:40 发布