how to exchnage bits in a byte
b7<-->b0 b6<-->b1 b5<-->b2 b4<-->b3
please mail me the code if any one know to
rajeshmb4u@gmail.com
Answer Posted / abdur rab
You can swap swap bits using two ways
1 ) with for loop
2 ) using recursion
#include <stdio.h>
char swap_bits_in_byte ( unsigned char byte_2_swap )
{
unsigned char swapped_byte = 0;
int nloop = 0;
for( nloop = 0; nloop < 8; ++nloop ) {
swapped_byte = swapped_byte << 1;
swapped_byte |= ( byte_2_swap & 1 );
byte_2_swap = byte_2_swap >> 1;
}
return ( swapped_byte );
}
unsigned char swap_bits ( unsigned char byte_2_swap, int
n_size )
{
unsigned char swapped_byte = 0;
int bits = ( ( sizeof ( unsigned char ) * 8 ) - 1 );
if ( bits == n_size ) {
swapped_byte = ( byte_2_swap
& (unsigned char) ( pow (
2, n_size ) ) )
? ( 1 << ( bits - n_size ) ) : 0;
} else {
swapped_byte = swap_bits ( byte_2_swap,
n_size + 1 );
swapped_byte |= ( byte_2_swap
& (unsigned char) ( pow (
2, n_size ) ) )
? ( 1 << ( bits - n_size ) ) : 0;
}
return ( swapped_byte );
}
int main ( int argc, char* argv [] )
{
unsigned char byte = 128 | 32;
unsigned char swapped_byte = 0;
swapped_byte = swap_bits_in_byte ( byte );
printf ( "\n Un Swapped Byte :%d", byte );
printf ( "\n Swapped Byte :%d", swapped_byte );
swapped_byte = swap_bits ( byte, 0 );
printf ( "\n Un Swapped Byte :%d", byte );
printf ( "\n Swapped Byte :%d", swapped_byte );
}
| Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
Given two strings S1 and S2. Delete from S2 all those characters which occur in S1 also and finally create a clean S2 with the relevant characters deleted.
How do we make a global variable accessible across files? Explain the extern keyword?
What is structure of c program?
Explain pointers in c programming?
Can two or more operators such as and be combined in a single line of program code?
What should malloc() do?
Is it acceptable to declare/define a variable in a c header?
What is the use of getch ()?
Which one to choose from 'initialization lists' or 'assignment', for the use in the constructor?
Dont ansi function prototypes render lint obsolete?
What is the use of putchar function?
What is a char c?
What is the difference between the local variable and global variable in c?
What is property type c?
What is adt in c programming?