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

What are the salient features of c languages?

0 Answers  


while initialization of two dimensional arrays we can initialize like a[][2] but why not a[2][] is there any reason behind this?

4 Answers   Aptech,


Is c language still used?

0 Answers  


Why are some ANSI/ISO Standard library routines showing up as undefined, even though I've got an ANSI compiler?

0 Answers   Celstream,


What is the scope of static variables in c language?

0 Answers  






What is the difference between the expression “++a” and “a++”?

0 Answers  


who developed c and why he developed c?

5 Answers  


1) There is a singing competition for children going to be conducted at a local club. Parents have been asked to arrive at least an hour before and register their children’s names with the Program Manager. Whenever a participant registers, the Program Manager has to position the name of the person in a list in alphabet order. Write a program to help the Program Manager do this by placing the name in the right place each time the Program Manger enters a name. 2) the Event Manager has to send participants to the stage to perform in the order in which they registered. Write a program that will help the Event Manager know who to call to the stage to perform. The Logic should be in Data Structures

0 Answers   KPIT,


write a program to create a sparse matrix using dynamic memory allocation.

0 Answers  


A program to allow an input operand and operator from the operator and read on the display and output operand.

0 Answers  


what does ‘segmentation violation’ mean?

1 Answers  


write a program to find lcm and hcf of two numbers??

1 Answers  


Categories