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 is difference between strcmp & palindrome?

3 Answers  


What is pointers in c with example?

0 Answers  


what is the most appropriate way to write a multi-statement macro?

1 Answers  


what is c?

4 Answers   IBM, TCS,


Explain how can you restore a redirected standard stream?

0 Answers  






Explain how can I remove the trailing spaces from a string?

0 Answers  


What is data structure in c and its types?

0 Answers  


how to swap four numbers without using fifth variable?

2 Answers  


Describe the difference between = and == symbols in c programming?

0 Answers  


an expression contains relational operators, assignment operators, and arithmatic operstors. In the absence of parentheses, they will be evaluated in which of the following order a) assignment, relational, arithematic b) arithematic, relational, assignment c) relational, arithematic, assignment d) assignment, arithematic, relational

0 Answers  


How do I convert a string to all upper or lower case?

0 Answers  


Define a structure to store the record of library. The record must consist of at least following fields: Title, Author, Edition, Price, Publisher, and Category. -Define functions authorSearch ( ), TitleSearch ( ) and CategorySearch ( ) to search a book with respect to author, title and category. [There can be more than one book, written by one author, in one category]

2 Answers  


Categories