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...

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


Please Help Members By Posting Answers For Below Questions

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.

1355


How do we make a global variable accessible across files? Explain the extern keyword?

1827


What is structure of c program?

1078


Explain pointers in c programming?

1063


Can two or more operators such as and be combined in a single line of program code?

1310


What should malloc() do?

1101


Is it acceptable to declare/define a variable in a c header?

1060


What is the use of getch ()?

1037


Which one to choose from 'initialization lists' or 'assignment', for the use in the constructor?

1019


Dont ansi function prototypes render lint obsolete?

1062


What is the use of putchar function?

1008


What is a char c?

994


What is the difference between the local variable and global variable in c?

927


What is property type c?

1045


What is adt in c programming?

1101