reverseNthBit(unsigned x, unsigned n)
{
unsigned y = 1 << n;
unsigned z = x^y;
printf("The number before %u, the number is %u after reversing bit number %u\n", x, z, n);
}
reverseNthBitInCharArray(char a[], unsigned n)
{
unsigned N = sizeof(a);
unsigned CharNum = n / 8;
unsigned bitNum = n % 8;
a[CharNum] = a[CharNum] ^ (1 << bitNum);
printf("char is %c, CharNum is %u, BitNumber is %u\n", a[CharNum], CharNum, bitNum);
}
reverseBitsinChar(unsigned char a)
{
unsigned char m, n;
unsigned char tmp = 1;
printf ("before reversal char is %u ", a);
for (unsigned i = 0; i < 4; i++)
{
m = a & (tmp << i);
n = a & (tmp << (7 - i));
if ((m > 0 && n > 0) || (m == 0 && n == 0))
{
continue;
}
else
{
if (m > 0)
{
a &= (~(1 << i));
a = (a | (1 << (7-i)));
}
else
{
a |= ((1 << i));
a &= (~(1 << (7-i)));
}
}
}
printf ("after reversal char is %u \n", a);
}
int main(void) {
//int arr[] = {1, 2, 4};
//int n = sizeof(arr)/sizeof(arr[0]);
//printf ("The odd occurring element is %d ", findOdd(arr, n));
char arr[] = {'a', 'b', 'c', 'd', 'e'};
unsigned int x = 77;
unsigned int n = 2;
reverseNthBit(x, n);
reverseNthBitInCharArray(arr, 30);
reverseNthBitInCharArray(arr, 34);
reverseBitsinChar(23);
reverseBitsinChar(13);
reverseBitsinChar(8);
return 0;
}
// Output: The odd occurring element is 90