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 standard functions are available to manipulate strings?

0 Answers  


Can a file other than a .h file be included with #include?

0 Answers   Aspire, Infogain,


DIFFERNCE BETWEEN THE C++ AND C LANGUAGE?

2 Answers   Wipro,


Explain enumerated types in c language?

0 Answers  


How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

2 Answers   CMC, Wipro,






What are the types of arrays in c?

0 Answers  


How can you determine the size of an allocated portion of memory?

0 Answers   Aspire, Infogain,


we called a function and passed something do it we have always passed the "values" of variables to the called function. such functions calles are called a) calls by reference b) calls by value c) calls by zero d) none of the above

0 Answers  


what is the use of bitfields & where do we use them?

2 Answers  


write C code to reverse a string such that if i/p is "abc defg hij klmno pqrs tuv wxyz" and the o/p should be "cba gfed jih onmlk srqp vut zyxw"

2 Answers  


In C programming, what command or code can be used to determine if a number of odd or even?

0 Answers  


#include <stdio.h> int main() { int i; for (i=0;i<3;++i) { fork();fork(); } } How many processes are created when running this program (including the initial one)? Explain &#1567;&#1567;&#1567;

4 Answers  


Categories