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
Answers were Sorted based on User's Feedback
Answer / 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 |
#include<stdio.h>
#include<conio.h>
int main()
{
int i,number,count=0,a[100];
printf("Enter the number\n");
scanf("%d",&number);
for(i=7;i>=0;i--)
{
if((1<<i) & number)
a[count] = 1;
else
a[count] = 0;
count++;
}
printf("Binary Value of the Given Number is:\n");
for(i=0;i<=7;i++)
{
printf("%d",a[i]);
}
printf("\nReversed Binary Value of the Given Number is:\n");
for(i=0;i<=7;i++)
{
printf("%d",a[7-i]);
}
printf("\n");
}
Is This Answer Correct ? | 0 Yes | 3 No |
Which is not valid in C a) class aClass{public:int x;}; b) /* A comment */ c) char x=12;
What is the difference between macros and inline functions?
What is structure data type in c?
What does a run-time "null pointer assignment" error mean?
why u join this call center?
program to find the second largest word in a paragraph amongst all words that repeat more thn twice
What is the acronym for ansi?
write a function that accepts an array A with n elements and array B with n-1 elements. Find the missing one in array B,with an optimized manner?
what is used instead of pointers in java than c?
write a program to display the numbers in the following 4 4 3 3 2 2 1 1 0 1 1 2 2 3 3 4 4
Draw a flowchart to produce a printed list of all the students over the age of 20 in a class .The input records contains the name and age of students. Assume a sentinel value of 99 for the age field of the trailer record
diff between exptected result and requirement?