Thursday, July 27, 2006

C FAQ - II

C FAQ – II

Bitwise operations

#include<stdio.h>

unsigned int swap( unsigned int p, int xp, int xy);
unsigned int setbit (unsigned int p, int xp);
unsigned int resetbit (unsigned int p, int xp);
unsigned int countbits (unsigned int p);

int main()
{
printf ("The values after swapping of 0xf001 is %x\n", swap (0xf001, 1, 15));
printf ("The values after setting of 0xf001 is %x\n", setbit (0xf001, 4));
printf ("The values after resetting of 0xf001 is %x\n", resetbit (0xf001, 4));
printf ("The values after counting of 0xf001 is %x\n", countbits (0xf001));
}

unsigned int swap( unsigned int p, int xp, int yp)
{
unsigned int x = p & (1 << xp);
unsigned int y = p & (1 << yp);

p = p ^ x ^ y; /* Reset those bits to zero */
if (yp > xp)
{
y = y >> (yp -xp);
x = x << (yp - xp);
y = x | y;
}
else
{
y = y >> (xp -yp);
x = x << (xp - yp);
y = x | y;
}
return (p | y);
}

unsigned int setbit (unsigned int p, int xp)
{
xp = 1 << xp;
return (p | xp);
}

unsigned int resetbit (unsigned int p, int xp)
{
xp = 1<< xp;
return (p & ~xp);
}

unsigned int countbits (unsigned int p)
{
int cnt=0;
while (p)
{
if (p & 1)
cnt++;
p = p>>1;
}
return cnt;
}

Output:

The values after swapping of 0xf001 is 7003
The values after setting of 0xf001 is f011
The values after resetting of 0xf001 is f001
The values after counting of 0xf001 is 5


FIFO Implementation

#include<stdio.h>

#define MAX 30

int FIFO[MAX];
int wptr,rptr;
int bcross;
int readfifo (int data);
int writefifo (int data);

int main()
{
int i;
int p;
for (i=0;i<MAX+3;i++)
writefifo (i);
for (i=0;i<MAX+3;i++)
printf ("data read from the fifo is %d\n", readfifo (i));
for (i=0;i<5;i++)
writefifo (i);
for (i=0;i<6;i++)
printf ("data read from the fifo is %d\n", readfifo (i));

}

int writefifo (int data)
{
if (wptr == rptr && (bcross == 1))
{
printf("Fifo Overflow detected \n");
return -1;
}
else if (wptr >= rptr && (bcross == 0))
{
FIFO[wptr++] = data;
}
else if (wptr < rptr && (bcross == 1))
{
FIFO[wptr++] = data;
}
else {
printf("FIFO corruption detected \n");
}
if (wptr == MAX)
{
wptr=0;
bcross=1;
}
return wptr;
}

int readfifo (int data)
{
if (wptr == rptr && (bcross == 0))
{
printf("Fifo Underrun detected \n");
return -1;
}
else if (rptr > wptr && (bcross == 1))
{
data= FIFO[rptr++];
}
else if (rptr <= wptr && (bcross == 0))
{
data=FIFO[rptr++];
}
else if (rptr == wptr && bcross == 1) {
data=FIFO[rptr++];
}
else {
printf( "Read corrupt \n");
printf("FIFO corruption detected \n");
}
if (rptr == MAX)
{
rptr=0;
bcross=0;
}
return data;
}

Output:
Fifo Overflow detected
Fifo Overflow detected
Fifo Overflow detected
data read from the fifo is 0
data read from the fifo is 1
data read from the fifo is 2
data read from the fifo is 3
data read from the fifo is 4
data read from the fifo is 5
data read from the fifo is 6
data read from the fifo is 7
data read from the fifo is 8
data read from the fifo is 9
data read from the fifo is 10
data read from the fifo is 11
data read from the fifo is 12
data read from the fifo is 13
data read from the fifo is 14
data read from the fifo is 15
data read from the fifo is 16
data read from the fifo is 17
data read from the fifo is 18
data read from the fifo is 19
data read from the fifo is 20
data read from the fifo is 21
data read from the fifo is 22
data read from the fifo is 23
data read from the fifo is 24
data read from the fifo is 25
data read from the fifo is 26
data read from the fifo is 27
data read from the fifo is 28
data read from the fifo is 29
Fifo Underrun detected
data read from the fifo is -1
Fifo Underrun detected
data read from the fifo is -1
Fifo Underrun detected
data read from the fifo is -1
data read from the fifo is 0
data read from the fifo is 1
data read from the fifo is 2
data read from the fifo is 3
data read from the fifo is 4
Fifo Underrun detected
data read from the fifo is -1

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home