Untitled
unknown
plain_text
2 years ago
884 B
9
Indexable
#include <stdio.h>
#include <math.h>
int nr_bits (unsigned int n){
int nr_bits = 0;
while (n > 0){
nr_bits++;
n = n >> 1;
}
return nr_bits;
}
int trailingZ (unsigned int n) {
int nr_zeros = 0;
int expoente = nr_bits(n) - 1;
int nr_bits_restantes = expoente + 1;
while (n % (int) pow(2, expoente) != 0){
expoente--;
nr_bits_restantes--;
}
while (nr_bits_restantes > 0){
if ((n & 1) == 0) {
nr_zeros++;
}
n = n >> 1;
nr_bits_restantes--;
}
return nr_zeros;
}
int main(){
int n = 12; // por exemplo, 12.
printf("\nO numero de bits a 0 no final da representacao binaria de %d (i.e., o expoente da maior potência de 2 que é divisor de %d): %d\n\nNrº de bits total: %d\n\n", n, n, trailingZ(n), nr_bits(n));
return 0;
}Editor is loading...
Leave a Comment