A 32 bit number is a series of bits from 0 to 31. We can access each bit via shifting operation. We take an unit 1 as reference min value to traverse bits. When we shift 1 by 0 at right, it positioned at 0th position and if we AND it with bits it gives 0th position value. ANDing with 1 gives us the exact value. If the bit is 1 then we get 1 else 0 will result as 0 only. Now we shift to next position i.e. shift right by 1 and AND with bits. This way we can iterate till 31th bit and obtain each bits.

Previous steps shows how to access each bit and how to iterate through bits. Now reversing neighbour bits are more complex. We need to get bit 0 and bit 1 and swap it and save it again back to bitmask. In the next iteration we access 1 and 2 th bit and swap. In this way we swap two bits till highest position. This is the way to swap 2 bits using a loop.

There is another way to swap bits without a loop. We take a bit mask with all even positions on (0x55). Now AND it with bits. This will get all even bits and shift it to next left. Again take a bit mask where all odd bits are on. AND it to mask will produce a mask of all odd bits. Now shift the bits to next right. Now finally or this two bit patters to get the result bits.

#include <stdio.h>
#ifdef USING_LOOP
unsigned int swap_2bits(unsigned int bits)
{
  int i;
  unsigned int ml, mh;
  for (= 0; i < 32; i += 2) {
    /* Get bit lower */
10      ml = (bits & (<< i));
11      /* Get bit higher */
12      mh = (bits & (<< (i + 1)));
13      /* Move lower to higher */
14      ml <<= 1;
15      /* Move higher to lower */
16      mh >>= 1;
17      /* reset two bits */
18      bits &= ~(0x3 << i);
19      /* copy two swapped bits back to bits */
20      bits |= (mh | ml);
21    }
22    return bits;
23  }
24  #else
25  unsigned int swap_2bits(unsigned int bits)
26  {
27    unsigned int mask = 0x55555555;
28    bits = ((bits & mask) << 1) | ((bits & ~mask) >> 1);
29    return bits;
30  }
31  #endif
32  void print_bits(unsigned int bits, int size)
33  {
34    int i;
35    char output[33];
36    memset(output, 0, sizeof(output));
37    for(= size-1; i >= 0; i--)
38    {
39      output[size-i-1] = (bits & (<<i))? '1' : '0';
40    }
41    printf(output);
42  }
43  int main(int argc, char* argv[])
44  {
45    unsigned int  bits;
46    bits = 0x12345678;
47    printf("Original Bits\n");
48    print_bits(bits, 32);
49    printf("\nReversed Bits\n");
50    bits = swap_2bits(bits);
51    print_bits(bits, 32);
52  }  
53 
Output
Original Bits
00010010001101000101011001111000
Reversed Bits
00100001001110001010100110110100

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

logical vs bitwise operators, logical and bitwise or, bit shifting, obtain bit mask, reverse bits, swap bits, bit set, reset/clear, toggle, xor operator, bitwise shifting, Operator precedence,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan