mercredi 6 janvier 2016

C random numbers that logic bit sum(OR) is equal to max unsigned short value

Task is to write a program that counts an avarage number of unsigned short ints needed to make their bit logical sum(a1 OR a2 OR a3…) be equal to 111..1. I'd really be thankful for any kind of help.

#include <stdio.h>
#include <time.h>

int main(){

    int count=0;
    int sum=0;
    srand(time(NULL));
    unsigned short r = rand();

    while(sum=255){
        sum = sum | r;
        count++;
        return count;
    }
    printf("the answer is : %d numbers\n", count);

    return 0;
}




Aucun commentaire:

Enregistrer un commentaire