Vijaynetwork Links

Home

C Programs


Custom Search

Reverse Bits - 16 bits

int main( void )
{
	int a= 0xFF00, i, rev=0;

	for( i = 0; i < 16; ++i )
	{
		if( a & ( 1 << i ) )
		{
			rev |= ( 0x8000 >> i );
		}
	}
	printf( "Input is 0x%04x\n", a );
	printf( "Reverse Bit Is 0x%04x\n", rev );

	return 0;
}

String Reverse


int main( void )
{
	char 	S[] = "iVijay C Programming";
	int  	i, j;

	for( i=0, j=strlen(S)-1; i<((strlen(S))/2); ++i, --j )
	{
	     S[i]  ^= S[j] ^=  S[i]  ^= S[j];
	}
	printf( "Reverse Is : %s\n", S );

	return 0;
}

String Length



int strlen( char *s )
{
	char *ptr = s;

	while( *ptr++ );
	return( ptr-s );
}

String Copy


char *strcpy( char *s, char *t )
{
	char *ptr=s;
	while( *s++ = *t++ );
	return( ptr );
}

String Cat


char *strcat( char *s, char *t )
{
	char *ptr=s;

	while( *s++ );
	while( *s++ = *t++ );
	return( ptr );
}

String Compare


int strcmp( char *s, char *t )
{
	int n;
	while ((n = *s - *t++) == 0 && *s++);
	return( n );
}

Nibble Swap & Alternate Bit Swapping


int main( void )
{
	unsigned char  a = 40, b=20;

	a = ( a>>4 ) | ( a<<4 );

	b = ( ( b & 0xAA ) >> 1 ) | ( ( b & 0x55 ) << 1 );
	
	printf( "After Nibble Swap %d\n", a );
	printf( "Alternate Bit swapping %d\n", b );
	getch();

	return 0;
}

Finding the middle of the linked list


Node *FindMiddle( Node *head )
{
	Node *p1, p2;
	p1 = p2 = head;
	while( p2 && p2->next )
	{
		p2 = p2->next->next;
		p1 = p1->next;
	}
	return( p1 );
}


VIJAY NETWORK