Skip to content

Instantly share code, notes, and snippets.

@munckymagik
Created August 2, 2020 09:48
Show Gist options
  • Save munckymagik/f8d5539f3cf708dcb3c1a6e5683b7201 to your computer and use it in GitHub Desktop.
Save munckymagik/f8d5539f3cf708dcb3c1a6e5683b7201 to your computer and use it in GitHub Desktop.
Visualising Redis' SCAN iteration
#include <iostream>
#include <bitset>
static uint8_t rev(uint8_t v) {
uint8_t s = 8 * sizeof(v); // bit size; must be power of 2
uint8_t mask = ~0;
while ((s >>= 1) > 0) {
mask ^= (mask << s);
v = ((v >> s) & mask) | ((v << s) & ~mask);
}
return v;
}
static void printval(std::ostream& io, char const * const label, const uint8_t val) {
std::bitset<sizeof(uint8_t) * 8> binval(val);
io << label << " = " << int(val) << " (" << binval << "), ";
}
int main() {
// for (uint8_t i = 0; i < 100; ++i) {
// uint8_t revd = rev(i);
// uint8_t revincrd = revd + 1;
// uint8_t incrd = rev(revincrd);
// printval(std::cout, "i", i);
// printval(std::cout, "revd", revd);
// printval(std::cout, "revincrd", revincrd);
// printval(std::cout, "incrd", incrd);
// std::cout << std::endl;
// }
std::cout << std::endl;
uint8_t val = 0;
for (auto i = 0; i < 100; ++i) {
printval(std::cout, "val", val);
std::cout << std::endl;
val = rev(val);
++val;
val = rev(val);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment