Tuesday, July 12, 2011

Bitwise XOR Operator

A number XORed with the another number twice will give the original number.I tried this sample program in Linux and it working fine.

#include
void main()
{
int i=23,j;
i=i^50;
printf("\nAfter 1st shift the value of i = %d\n",i);
i=i^50;
printf("\nAfter 2nd shift the value of i = %d\n",i);
if(i==23)
printf("\nInitial value= Final Value ---> SUCCESS\n");
else printf("\nSOMETHING WRONG IN THE PROGRAM\n");
}


The output will be

After 1st shift the value of i = 37

After 2nd shift the value of i = 23

Initial value= Final Value ---> SUCCESS

Thursday, July 7, 2011

Sorting in C - Descending

Same as the previous program just to change the condition in the if the statement.

if(a[i]<=a[j])----->if(a[i]>=a[j])

You will get the output.

#include
#include
int main()
{
int i,j,temp;
int a[5]={-123,34,14,87,45};
printf("SORTING PROGRAM IN DESCENDING\n");
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>=a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
} for(i=0;i<5;i++)
printf("%d\n",a[i]);
getch();
}

Sorting in C - By Ascending

And finally i come up with a basic of C programmer for new buddies.Hope it might be useful.

I had given 5 values in the array to be simple.

#include
#include
int main()
{
int i,j,temp;
int a[5]={-123,34,14,87,45};
printf("SORTING PROGRAM IN ASCENDING\n");
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]<=a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
} for(i=0;i<5;i++)
printf("%d\n",a[i]);
getch();
}