Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- uint8_t clz(uint32_t test, uint8_t bits) {
- uint32_t cut = (test >> (32 - bits)) << (32 - bits);
- if (cut == 0) {
- return bits;
- }
- if (bits == 0) {
- return 0;
- }
- uint8_t half = bits/2;
- uint8_t result = clz(cut, half);
- if (result == half) {
- return half + clz(cut << half, half);
- } else {
- return result;
- }
- }
- int main() {
- int c;
- uint32_t a = 0xffffffff;
- for (c=0; c<32; c++) {
- printf("%08x %u\n", a, clz(a, 32));
- a = a >> 1;
- }
- printf("%08x %u\n", a, clz(a, 32));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement