bit Field manipulation [C]

a program demonstrating the different ways to manipulate bits
xor-^
or-|
and-&

some common library functions have been rewritten in an attempt to avoid the standard libraries as much as possible. (for educational purposes)

read(),write() are unix system calls writing to file descriptors
the standard file descriptors open by default are:
0-stdin
1-stdout
2-stderr

the different modes:
m0-use xor
m1-use |,&
m2-assign a number to the 8 bit wide bit field, entering 256 will cause it to overflow, displaying all zeros

#include <stdlib.h>
#include <unistd.h>

void bon(size_t,char*);
void boff(size_t,char*);
void btog(size_t,char*);
void bprint(char*);
int bchek(size_t,char*);
int charLen(char*);
int charInt(char*);
int memcompare(char*,char*,size_t);

int main()
{
	char *buff=(char*)malloc(10); //80 bits
	char *dump=(char*)malloc(100); //800 bits
	char *bfld=calloc(1,1);
	int i;
	int j=0;

	#define INSTR1 "enter: [0-7],print,quit,m[0-2]\nm0:xor\tm1:or/and\tm2:assign #\n"
	write(1,INSTR1,sizeof(INSTR1));

	while(1)
	{
		if(read(0,buff,10)==10) 
		{
			if(*(buff+9)!=0xa) while(100==read(0,dump,100));
			else *(buff+9)=0x0;
		}

		if(memcompare("quit\n",buff,5)==0) exit(0);
		else if(memcompare("print\n",buff,6)==0)
		{
			bprint(bfld);
			continue;
		}
		else if(*buff=='m' && *(buff+1)>47 && *(buff+1)<51)
		{
			if(*(buff!=0xa)
			{
				#define INSTR2 "invalid input\n"
				write(1,INSTR2,sizeof(INSTR2));
				continue;
			}
			j=charInt((buff+1));
			continue;
		}
		else
		{
			if(j==0)
			{
				if(!(*buff>47 && *buff<56) || *(buff+1)!=0xa)
				{
					write(1,INSTR2,sizeof(INSTR2));
					continue;
				}
			}
			else if(j==1)
			{
				if(!(*buff>47 && *buff<56) || *(buff+1)!=0xa)
				{
					write(1,INSTR2,sizeof(INSTR2));
					continue;
				}
			}
		}

		i=charInt(buff);

		if(j==0) btog(i,bfld);
		else if(j==1)
		{
			if(bchek(i,bfld)==0) bon(i,bfld);
			else boff(i,bfld);
		}
		else *bfld=i;

		bprint(bfld);
	}

	return 0;

}

void bon(size_t i,char *bfld)
{
	*bfld|=(1<<i);
}
void boff(size_t i,char *bfld)
{
	*bfld&=~(1<<i);
}
void btog(size_t i,char *bfld)
{
	*bfld^=(1<<i);
}
int bchek(size_t i,char *bfld)
{
	if((*bfld&(1<<i))==0) return 0;
	else return 1;
}
void bprint(char *bfld)
{
	char i,b[9]={0};
	for(i=0;i<8;i++)
	{
		if(bchek(7-i,bfld)==0) b[i]=48;
		else b[i]=49;
	}
	b[++i]=0xa;
	write(1,&b,10);
}

int charLen(char *c)
{
	int i=0;
	while(*(c+i)!=0xa && *(c+i)!=0x0) 
	{
		i++;
	}
	return i;
}
int charInt(char *c)
{
	int i,j,k=0,l=1;

	for(j=charLen(c)-1;j!=-1;j--)
	{
		switch(*(c+j))
		{
			case '0': i=0; break;
			case '1': i=1; break;
			case '2': i=2; break;
			case '3': i=3; break;
			case '4': i=4; break;
			case '5': i=5; break;
			case '6': i=6; break;
			case '7': i=7; break;
			case '8': i=8; break;
			case '9': i=9; break;
		}
		k+=i*l;
		l*=10;
	}
	return k;
}
int memcompare(char *str1,char *str2,size_t j)
{
	int i;
	for(i=0;i<j;i++)
	{
		if(*(str1+i)!=*(str2+i)) return i+1;
	}
	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *