Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
get uint32_t length
#include <stdlib.h>
#include <stdio.h>
void print_bits(uint32_t x);
uint32_t uint32_smear_right(uint32_t x){
x = x | x>>1;
x = x | x>>2;
x = x | x>>4;
x = x | x>>8;
x = x | x>>16;
return x;
}
int uint32_pop(uint32_t x){
x = ( x & 0x55555555 ) + ( (x >> 1 ) & 0x55555555 );
x = ( x & 0x33333333 ) + ( (x >> 2 ) & 0x33333333 );
x = ( x & 0x0F0F0F0F ) + ( (x >> 4 ) & 0x0F0F0F0F );
x = ( x & 0x00FF00FF ) + ( (x >> 8 ) & 0x00FF00FF );
x = ( x & 0x0000FFFF ) + ( (x >> 16) & 0x0000FFFF );
return x;
}
int uint32_length(uint32_t x){
x = uint32_smear_right(x);
return uint32_pop(x);
}
uint32_t uint32_smear_right_print(uint32_t x){
x = x | x>>1;
printf("Set every bit that is 1 place to the right of a 1\n");
print_bits(x);
printf("\n");
x = x | x>>2;
printf("Set every bit that is 2 places to the right of a 1\n");
print_bits(x);
printf("\n");
x = x | x>>4;
printf("Set every bit that is 4 places to the right of a 1\n");
print_bits(x);
printf("\n");
x = x | x>>8;
printf("Set every bit that is 8 places to the right of a 1\n");
print_bits(x);
printf("\n");
x = x | x>>16;
printf("Set every bit that is 16 places to the right of a 1\n");
print_bits(x);
printf("\n");
return x;
}
int uint32_pop_print(uint32_t x){
x = ( x & 0x55555555 ) + ( (x >> 1 ) & 0x55555555 );
printf("Accumulate pop counts of bit buckets size 2\n");
print_bits(x);
printf("\n");
x = ( x & 0x33333333 ) + ( (x >> 2 ) & 0x33333333 );
printf("Accumulate pop counts of bit buckets size 4\n");
print_bits(x);
printf("\n");
x = ( x & 0x0F0F0F0F ) + ( (x >> 4 ) & 0x0F0F0F0F );
printf("Accumulate pop counts of bit buckets size 8\n");
print_bits(x);
printf("\n");
x = ( x & 0x00FF00FF ) + ( (x >> 8 ) & 0x00FF00FF );
printf("Accumulate pop counts of bit buckets size 16\n");
print_bits(x);
printf("\n");
x = ( x & 0x0000FFFF ) + ( (x >> 16) & 0x0000FFFF );
printf("Accumulate pop counts of bit buckets size 32\n");
print_bits(x);
printf("\n");
return x;
}
uint32_t print_4_bits(uint32_t x){
int i,res;
for(i=0;i<4;i++){
printf("%u",(x&0x80000000)>>31);
x=x<<1;
}
return x;
}
void print_bits(uint32_t x){
int i;
for(i=0;i<8;i++){
x=print_4_bits(x);
printf(" ");
}
}
void print_length_info(uint32_t X){
int res;
uint32_t X2;
print_bits(X);
printf("\n");
X2=uint32_smear_right_print(X);
res=uint32_pop_print(X2);
printf("\n");
printf("The length of %u is %d bits\n",X,res);
}
int main(){
uint32_t X=0x007F8A24;
uint32_t Y=0x000030AF;
printf("X=");
print_length_info(X);
printf("\n");
printf("Y=");
print_length_info(Y);
uint32_t x=X;
x = x | x>>1;
x = x | x>>2;
x = x | x>>4;
x = x | x>>8;
x = x | x>>16;
x = ( x & 0x55555555 ) + ( (x >> 1 ) & 0x55555555 );
x = ( x & 0x33333333 ) + ( (x >> 2 ) & 0x33333333 );
x = ( x & 0x0F0F0F0F ) + ( (x >> 4 ) & 0x0F0F0F0F );
x = ( x & 0x00FF00FF ) + ( (x >> 8 ) & 0x00FF00FF );
x = ( x & 0x0000FFFF ) + ( (x >> 16) & 0x0000FFFF );
printf("Alternatively, use built-function\n");
int resX=32-__builtin_clz(X);
int resY=32-__builtin_clz(Y);
printf("Length X: %d\nLength Y: %d\n",resX,resY);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.