Setting a bit

Use the bitwise OR(|) operator to set a bit, this function return 1 if value was changed

char SetBit(int *Value,int bit)
{
	char cret;
	cret=GetBit(*Value, bit);//get a bit
	if(!cret)//if it is cleared
	{
		*Value=(*Value | 1<<bit); //test a bit
		cret=1;//Value was changed, return 1 
	}

	return cret;
}

Clearing a bit
This function test a bit passed , if it is up execute a difference between Value and 2^bit, return 1 if value was changed

char ResBit(int *Value,int bit)
{
	char cret;
	cret=0;
	if(GetBit(*Value, bit))//test a bit
	{
	   *Value=*Value-(1<<bit);
	   cret=1;
	}
	return  cret;
}

Getting a bit
This function return value of bit passed

int GetBit(int Value,int bit)
{
   return (Value & 1<<bit)>0;
}

Toggling a bit
Using the previous functions, this function invert status of bit of value parameter

char Toggle(int *dest,char Value,int bit)
{
	char cret;

	if(GetBit(*dest,bit))
	{
		cret=ResBit(dest, bit);
	}
	else
	{
		cret=SetBit(dest, bit);
	}
	return cret;
}
Advertisements