Exercise: Turn decimal to binary and find 1’s

Asked

Viewed 478 times

-1

inserir a descrição da imagem aqui

#include<stdio.h>

int main()
{
int k,repeticao;
int valor, numero;
int paridade = 0;
scanf("%d", &repeticao);

for( k = 0; k < repeticao; k++ ){
    int numero,sequencia;
    scanf("%lld", &numero);
    valor = numero/2;
    while( valor != 0 ){
        sequencia = numero%2;
        if( sequencia == 1)
        {
            paridade++;
        }
        numero = valor;
        valor = numero/2;
        if (valor == 0)
    {
        paridade++;
    } 

    }

    printf("%d\n", paridade);
    paridade = 0;
}
return 0;

}

My code is working for the two entries given in the example: 3 and 21, but in the third entry it returns 15, instead of 50. this third entry is the repetition from 1 to 9 (3 times), and when I repeat it twice(123456789123456789) my output is 1( wrong too) when I put only one repetition(123456789) my output comes out correct ( 16). Can you help me, please?

  • Do you just want to count bits 1 of an integer? I think the code to do this is much simpler. Just make successive divisions and count when there is rest.

  • But I am making successive divisions and when the rest is 1 I am increasing the variable "parity". There must be a simpler way, but I can’t see, I’d also like to know why it doesn’t work for all cases the way it is there.

  • I’ll post my solution.

  • In Python it would be very easy to do this because the variables have no set size, already in C I believe it is not possible, nor the double supports a number with so many digits, the largest number supported has 16/17 digits since it is a variable of 8 bytes. Then it would not be possible to solve this case.

  • @Luanprado: Don’t say "in C is not possible", no; just do as Python does and use an arbitrary precision library, like the GMP and the MPFR. Incidentally, here is a list.

1 answer

2


Here is a simplified and properly commented solution to determine the number of bits of an integer. As it is an exercise and not a practical problem. I recommend that you study the code and understand how it works.

#include<stdio.h>

int main()
{
//variavel que recebera o inteiro
int a;
scanf("%d", &a);
//contador de bits setados como 1
int p = 0;

//enquanto a for maior que zero...
while (a > 0) {
    //verifica se a é impar
    if (a%2!=0) {
        p++;
    }
    //realiza divisões sucessivas 
    a = a/2;

}    

printf("Saída:%d",p);

}

Updating:

The calculation does not work for the third case as the value extrapolates the storage capacity of int, long, long int....

Prog.cpp:7:32: Warning: integer Constant is Too large for its type unsigned long long int a = 123456789123456789123456789;

  • 2

    when I put the third exercise entry ( 12345678912345678912345678789) is with the same error that my code had: says that there are 15 instead of 50 bits 1

  • Yes, an overflow occurs. In this case the problem is not in your solution but in the storage capacity of the integer. Try to use long int.

  • if I use long int, which flag should I use?

  • I will adapt the solution to calculate the number of the.

  • I tested with unsigned long long int and it is returning 35. I am not able to reproduce this calculation somewhere to confirm if it is 50 1s even.

  • I found this behavior very strange... but anyway , thank you so much for your help

  • Did whoever proposed this statement verified the feasibility of doing this in C? ... or maybe there is another technique that I do not know.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.