Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. uint8_t clz(uint32_t test, uint8_t bits) {
  5. uint32_t cut = (test >> (32 - bits)) << (32 - bits);
  6. if (cut == 0) {
  7. return bits;
  8. }
  9. if (bits == 0) {
  10. return 0;
  11. }
  12.  
  13. uint8_t half = bits/2;
  14. uint8_t result = clz(cut, half);
  15. if (result == half) {
  16. return half + clz(cut << half, half);
  17. } else {
  18. return result;
  19. }
  20. }
  21.  
  22. int main() {
  23. int c;
  24. uint32_t a = 0xffffffff;
  25. for (c=0; c<32; c++) {
  26. printf("%08x %u\n", a, clz(a, 32));
  27. a = a >> 1;
  28. }
  29. printf("%08x %u\n", a, clz(a, 32));
  30. return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement