Created
March 15, 2012 17:31
-
-
Save chintan09/2045487 to your computer and use it in GitHub Desktop.
bit reversal based on a mask given
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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