Skip to content

Instantly share code, notes, and snippets.

@chintan09
Created March 15, 2012 17:31
Show Gist options
  • Save chintan09/2045487 to your computer and use it in GitHub Desktop.
Save chintan09/2045487 to your computer and use it in GitHub Desktop.
bit reversal based on a mask given
// Input : bitReverseMasked(0x9, 1, 3)
// Output : 0x3 [ Reversing bit between low and high]
uint32_t bitReverseMasked(uint32_t val, uint8_t low, uint8_t high)
{
uint32_t mask;
uint32_t rev_val;
uint8_t i;
mask = ((1UL<<(high - low + 1)) - 1) << low;
rev_val = val & ~mask;
for (i=low; i<=high; i++) {
if ((val>>i) & 0x1) {
rev_val |= 1UL << (high - i + low);
}
}
return rev_val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment