There is a countBits function which i do not know how to complete.
Can someone show me with an explanation/comments
Code:
#include <stdio.h>
#define TEST(v,x) (countBits(v) == x) ? puts("Passed") :
puts("Failed")
//Function prototype
//unsigned char countBits(const unsigned char u);
//This function passes different values to the function countBits, and
checks the returned value is correct
// int main(int argc, const char * argv[]) {
puts("Begin tests");
TEST(0, 0); //Minimum edge case
TEST(1u, 1); //smallest for 1 result
TEST(1u<<1, 1);
TEST(1u<<7, 1); //largest for 1 result
TEST(0x03, 2); //Smallest for 2 result
TEST(0xC0, 2); //Largest for 2 result
TEST(0xFF, 8); //Maximum edge case
return 0;
}
//FUNCTION UNDER TEST
// Function countBits
//
// Return value: returns the number of '1's in the binary
representation of u
//
// Examples.
// If u = 12, this is represented as binary 00001100
// There are two '1's in this number, so the function will return 2
//
// If u = 13, this is represented as binary 00001101
// There are three '1's in this number, so the function will return 3
//
unsigned char countBits(const unsigned char u)
{
unsigned char numberOfBits = 0;
// BEGIN CODE HERE
return numberOfBits;
}
Also, could someone show me a simple example code to demonstrate the 'switch-case' and 'if-else if else' and statements with comments/ explanation?
And this code-
Code:
unsigned short x1;
/*
* The code below is supposed to request the user to input
a numeric value
* If the value is not between 20 and 50, it should ask
again
* If 20 <= x <= 50, then the code should proceed.
*
* where are the bugs?
* only use while loop
*/
do {
puts("Enter a whole number x, such that 20 <= x <=
50");
scanf("%hu", x1);
} while ( (x1>=20) && (x1 <50) );