Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Bit swapping

Answers were Sorted based on User's Feedback



Bit swapping..

Answer / eswar.rajan89

#include<stdio.h>

void printbits(unsigned int a)
{
int i;
int b;
for(i=31;i>=0;i--)
{
b=(a>>i) & 1;
if(b == 1)
printf("1");
else
printf("0");
}
}
int find_bit(int end_bit,int start_bit, int value)
{
int x1,i;
int x2=0,x3=0,x4=0;
for(i=start_bit;i<end_bit;i++)
{
x1=(value>>i)&1;
x2=(x2<<1) | x1;
}
for(i=0;i<end_bit;i++)
{
x3=(x2>>i)&1;
x4=(x4<<1) | x3;
}
return x4;
}

int main()
{
int a,b,c,d,val,rep_bit,main_bit,conv_bit,c1_bit;
printf("\nEnter a number 'A' and 'B' \n");
scanf("%d %d",&a,&b);
printf("\nEnter a number 'C' and 'D' \n");
scanf("%d %d",&c,&d);
val=b-a+1;
rep_bit = find_bit(val,0,d);
c1_bit = find_bit(a,0,c);
main_bit = rep_bit<<a;
printf("\nThe bits before replacement are:\n");
printbits(c);
printf("\n");
printbits(main_bit);
printf("\n");
conv_bit= main_bit | c1_bit;
printf("\nThe bit after replacement is:\n");
printbits(conv_bit);
printf("\n");
printf("\n");
return 0;
}

Is This Answer Correct ?    3 Yes 2 No

Bit swapping..

Answer / sibnath halder

Assuming that you want to use 8 bits of whatever bytes you
have, you could use

char swapbyte(unsigned char c)
{ unsigned char result=0;
for(int i=0;i<8;++i)
{ result=result<<1;
result|=(c&1);
c=c>>1;
}
return result;
}

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Interview Questions

How to write a program for swapping two strings without using 3rd variable and without using string functions.

7 Answers   iGate, Infotech,


Give the output for the following program. #define STYLE1 char main() { typedef char STYLE2; STYLE1 x; STYLE2 y; clrscr(); x=255; y=255; printf("%d %d\n",x,y); }

2 Answers   ADITI,


How can I call system when parameters (filenames, etc.) Of the executed command arent known until run time?

0 Answers  


Which of the following about the C comments is incorrect ? a.commentscan go over multiple lines b.comments can start any where in the line c.a line can contain comments with out any language statements d.comments can occur within comments

6 Answers   TCS,


How can I increase the allowable number of simultaneously open files?

1 Answers   CSC,


How can you check to see whether a symbol is defined?

0 Answers  


post new interiew question and aptitude test papers

1 Answers  


Is c object oriented?

0 Answers  


What is a #include preprocessor?

0 Answers  


#include<stdio.h> int main() { int i=0,j=1,k=2,m,n=0; m=i++&&j++&&k++||n++; printf("%d,%d,%d,%d,%d",i,j,k,m,n); }

12 Answers   Capital IQ, Sasken,


How would you sort a linked list?

1 Answers  


will the program compile? int i; scanf(ā€œ%dā€,i); printf(ā€œ%dā€,i);

3 Answers  


Categories