I am beginner to this programming and recently my teacher gave me a homework on swapping two bits in a byte. I could not figure out how to do so i referred to a website where there was an answer for that. The thing is there they gave a fixed input but I want to get input at runtime and swap bits in that.But I am receiving same output for every input
My code is
#include <stdio.h>
int main()
{
unsigned char data;
printf("Enter hex number: ");
scanf("%c",&data);
unsigned char bit_1 = (data >> 1) & 1;
unsigned char bit_2 = (data >> 2) & 1;
unsigned char xor_of_bit = bit_1 ^ bit_2;
printf("After swapping the bits, data value is: %2X", data ^ (xor_of_bit << 1 | xor_of_bit << 2));
return 0;
}
My input was 0xA and my ouyput was 30