Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) unsigned number1Bits( unsigned n ); Returns the number of 1-bits in n. For ex

ID: 3641124 • Letter: 1

Question


1) unsigned number1Bits( unsigned n );
Returns the number of 1-bits in n. For example, if main says
number1Bits( 0xBADDECAFU )
then the function returns 22. The function does no I/O, though of course main can output the returned value if it
wants.

2) unsigned number0Bits64( unsigned long long n );
Just like number0Bits, but now our arg is an unsigned long long. For example, if main says
number0Bits64( 0xFEEDFACEBADDECAFULL )
then the function returns 18.

3) unsigned longest0Run( unsigned n );
The function returns the largest number of consecutive 0-bits in n. For instance, if n’s bits were
11110111 00111111 10110100 00101111
the function would return 4 (because of the 4 underlined 0-bits).

4) int final( unsigned a, unsigned b );
This function returns 0 or 1. If a and b are identical, the function returns 0. Otherwise, the function
returns the most significant bit of b that is different from the corresponding bit in a.
For instance, suppose that the bits of a were
00101001 01110101 11011101 01011011
and that the bits of b were
00101001 01111011 00001111 11110000
The most significant bit of b that differs from a is bit #19 (underlined). Since bit #19 of b is 1, the function
returns 1. If b’s bit #19 were turned off, then the function would return 0, because b’s bit #18 is 0 (and is
different from a’s bit #18).

5) int prime( unsigned long long n );
This isn’t actually a bit-manipulation function, but it’s part of the job that the next function needs to do.
prime’s job is to return whether n is a prime number (so it always returns 0 or1). As you probably recall from
math, a prime number is a number greater than 1 with no positive factors except 1 and itself. The first 4 prime
numbers are 2, 3, 5, and 7. If you’ve written a function to test for primality before, you probably don’t need any
more direction. If not, though, here’s a pretty good, pretty simple, way to test a number for primality:
if n is less than 4, then it’s prime iff (if and only if) it’s greater than 1.
otherwise, if n is even, then it’s not prime
otherwise, test whether n is divisible by trial factors 3, 5, 7, 9, .... Keep going until you find a
trial factor that is an actual factor (in which case n isn’t prime, return 0) or until the trial factor becomes >= n
divided by the trial factor (in which case n is prime, return 1). If both conditions hold for the same trial factor
then n isn’t prime (it is, in fact, the square of an odd prime). So, we’re interested in both n % tf and n / tf. For
example, if we were checking n = 401 for primality, our loop would proceed as
tf n%tf n/tf
3 2 133
5 1 80
7 2 57
9 5 44
11 5 36
13 11 30
15 11 26
17 10 23
19 2 21
21 2 19
(At this point, since tf >= n/tf, we return 1. 401 is prime)
For example, if we were checking n = 403 for primality, our loop would proceed as
tf n%tf n/tf
3 1 134
5 3 80
7 4 57
9 7 44
11 7 36
13 0 31
(At this point, since n%tf is 0, we return 0. 403 is not prime.)
For example, if we were checking n = 25 for primality, our loop would proceed as
tf n%tf n/tf
3 1 8
5 0 5
(At this point, we have both conditions that we’re testing for. We ask the

question about n%tf before the question about n/tf, and so return 0. 25 is not
prime.)
BTW, to test your prime function for large values, you can write a little loop to find the largest prime unsigned
long long.
The largest unsigned long long is ULLONG_MAX (that’s a constant defined in the stdlib.h header file)
The largest unsigned long long minus the largest prime unsigned long long is 58. (That 58 number took my
desktop computer between 1 and 2 minutes to compute.)
To test your prime function for small values, you can write a little loop to find the sum of all prime numbers
less than 100. The total should be 1060.

6) unsigned long long setPrimeBits( unsigned long long base );
The caller wants us to answer the following 64 questions:
is base prime?
is base + 1 prime?
is base + 2 prime?
is base + 3 prime?
...
is base + 63 prime?
The function is supposed to build and return a 64-bit unsigned long long where each bit holds the answer to the
corresponding question: bit #k of the return value tells whether base + k is a prime number.
For instance, if the caller says
setPrimeBits( 100 )
then the function should return 0x820A00A08800228AULL. The highest-order bit is a 1, saying that
100+63 == 163 is prime
The lowest-order bit is 0, saying that
100+0 == 100 is not prime
Let’s say that if adding the base to any of the numbers [0,63] overflows, we’ll just use the 64-bit sum. For
instance, if base were ULLONG_MAX, then we’d ask the questions
is ULLONG_MAX prime? (answer is no)
is ULLONG_MAX + 1 == 0 prime? (answer is no)
is ULLONG_MAX + 2 == 1 prime? (answer is no)
is ULLONG_MAX + 3 == 2 prime? (answer is yes)
...
is ULLONG_MAX + 63 == 62 prime? (answer is no)
and we’d return 0x5041144141145158ULL (which is the result of taking setPrimeBits( 0 ) and shifting the
result left 1 bit). This way we don’t need any special test for overflow.

Explanation / Answer

please rate - thanks

with my version of prime

#include<stdio.h>
#include<conio.h>
unsigned number1Bits(unsigned);
unsigned number0Bits64(unsigned long long);
unsigned longest0Run(unsigned);
int final(unsigned,unsigned);
int prime( unsigned long long );
int main()
{
printf("%u ",number1Bits(0xBADDECAFU ));
printf("%u ",number0Bits64(0xFEEDFACEBADDECAFULL));
printf("%u ",longest0Run(0xF73FB44FU));
printf("%u ",final(0x2975DD5B,0x297B0FF0));
printf("%d ",prime(401));
getch();
return 0;
}
int prime( unsigned long long n )
{unsigned long long i;
   for(i=2;i<=n/2;i++)
     if(n%i==0)
         return 0;
return 1;     
}
int final( unsigned a, unsigned b )
{unsigned int mask=0x80000000,c,d;
int i;
if(a==b)
    return 0;
for(i=31;i>=0;i--)
   {c=a&mask;
    d=b&mask;
   if(c!=d)
       if(d==0)
           return 0;
       else
           return 1;
   mask=mask/2;
    }
}
unsigned number0Bits64(unsigned long long a)
{int i,sum=0;
unsigned long long mask=0x8000000000000000ULL,b;
for(i=0;i<64;i++)
{b=a&mask;
   if(b==0)
      sum++;
   mask=mask/2;
    }
return sum;

}

unsigned number1Bits(unsigned a)
{unsigned int mask=0x80000000,b;
int i,sum=0;
for(i=31;i>=0;i--)
   {b=a&mask;
   if(b!=0)
      sum++;
   mask=mask/2;
    }
return sum;
}
unsigned longest0Run(unsigned a)
{unsigned int mask=0x80000000,b;
int last=0,i,n=0;
for(i=0;i<32;i++)
{b=a&mask;
   if(b==0)
      if(last==0)
           n++;
       else
          last=0;
    else
        last=1;
   mask=mask/2;
    }

return n;
}