1
/* This software was developed by Bruce Hendrickson and Robert Leland *
2
* at Sandia National Laboratories under US Department of Energy *
3
* contract DE-AC04-76DP00789 and is copyrighted by Sandia Corporation. */
5
/* Reverse the bits of a number. */
6
int bit_reverse(int val, int nbits)
7
/* value to reverse bits of */
8
/* number of significant bits */
10
int mask_low, mask_high; /* masks for bits to interchange */
11
int bit_low, bit_high;/* values of the bits in question */
12
int i; /* loop counter */
15
mask_high = 1 << (nbits - 1);
16
for (i = 0; i < nbits / 2; i++) {
17
bit_low = (val & mask_low) >> i;
18
bit_high = (val & mask_high) >> (nbits - i - 1);
21
if (bit_low != bit_high) {
22
val ^= (1 << i) ^ (1 << (nbits - i - 1));