Question: An incomplete C program is given that asks an integer from the user and prints details about bitwise representation of the integer. Your task is

An incomplete C program is given that asks an integer from the user and prints details about bitwise representation of the integer. Your task is to complete two functions (named as 'number_of_setbits' and 'trailing_zeros') defined below and given in 'pe3.c' source file.
Note that there are two other functions ( 'printbitsofInteger' and 'leading_zeros') given for your reference. The function called 'printbitsofInteger' prints the bits of the stored integer value and the function called 'leading_zeros' returns the number of leading zeros of an integer.
Please do not edit anything other than the two function that you are expected to implement. Rest of the code is used to print results properly. See the sample result on the next page.
Instructions:
- Step 0: Copy the given file "pe3.c" to your working directory.
- Step 1: Open the given (incomplete) C source code (named as "pe3.c") with vim by using the command: vim pe3.c
(a) Write a C function ('number_of_setbits') to return the number of set bits (1s) in a given integer using bitwise operators.
(b) Write a C function ('trailing_zeros') to return the number of trailing zeros in a given integer using bitwise operators. The trailing zeros is the number of consecutive zero bits in the least significant part of the stored integer.
Note: The functions are already provided at the end of the given file ("pe3.c") but they are incomplete. You are expected to complete them.
- Step 2: Build your executable program named as "pe3" by using the command: gcc -o 3 pe3.c
- Step 3: Run the program by using the command: ./pe3
- Step 4: Test your code with the provided "test3.sh" file by using the command: ./test3.sh pe3.c
Note 1: You should have the "test3.sh" in the same directory as your source code.
Note 2: You should see "PASS" for all the test cases.
- Step 5: Submit your source code ("pe3.c") to Learn.Hub (assignment "PE3").
Sample Result
```
[Zafers-MacBook-Pro:src zafer$ ./pe3s
Enter any integer: 256
000000000000000000000000100000000
In 256, total number of set bits(1s) is =1, and unset bits (0s) is =31
Total number of leading zeros in 256 is =23.
Total number of trailing zeros in 256 is =8.
[Zafers-MacBook-Pro:src zafer$ ./pe3s
Enter any integer: 44
000000000000000000000000000101100
In 44, total number of set bits(1s) is =3, and unset bits (0s) is =29
Total number of leading zeros in 44 is =26.
Total number of trailing zeros in 44 is =2.
[Zafers-MacBook-Pro:src zafer$ ./pe3s
Enter any integer: 0
000000000000000000000000000000000
In 0, total number of set bits(1s) is =0, and unset bits (0s) is =32
Total number of leading zeros in 0 is =32.
Total number of trailing zeros in 0 is =32.
[Zafers-MacBook-Pro:src zafer$ ./pe3s
Enter any integer: -1
111111111111111111111111111111111
In -1, total number of set bits(1s) is =32, and unset bits (0s) is =0
Total number of leading zeros in -1 is =0.
Total number of trailing zeros in -1 is =0.
[Zafers-MacBook-Pro:src zafer$ ./pe3s
Enter any integer: -150
111111111111111111111111101101010
In -150, total number of set bits(1s) is =28, and unset bits (0s) is =4
Total number of leading zeros in -150 is =0.
Total number of trailing zeros in -150 is =1.
```
```
// DO NOT MODIFY ANYTHING OTHER THAN THE TWO
FUNCTIONS (number_of_setbits and trailing_zeros)
// NOTE THAT THE TWO FUNCTIONS THAT YOU WILL
IMPLEMENT ARE AT THE END OF THIS FILE
#include
#define TBITNO (int)sizeof(int)*8
int trailing_zeros(int num);
int number_of_setbits(int);
int leading_zros(int num);
void printbitsofInteger(int x);
int main(void)
{
/* Declare variables to get user input */
int num, ones, count =0;
/* Get user input from the keyboard. */
printf("Enter any integer: ");
scanf("%d", &num);
/* Print bits of the stored integer */
printbitsofInteger(num);
/* Compute number of set bits in num */
ones = number_of_setbits(num);
printf("In %d, ttal number of set bits(1s) is
=%d, and unset bits (Os) is =%d
", num, ones,
TBITNO - ones);
printf("Total number of leading zeros in %d is
=%d.
", num, leading_zeros(num));
printf("Total number of trailing zeros in %d is
=%d.
", num, trailing_zeros(num));
return 0;
}
void printbitsofInteger(int x)
{
int i;
for (i = TBITNO -1; i >=0; i--)
{
(x & (1 i))? putchar('1') :
putchar('0');
}
printf("
");
}
int leading_zeros(int num)
{
``````
18:58
```
printf("Total number of trailing zeros in \%d is =\%d.
", num, trailing_zeros(num));
return 0;
\}
void printbitsofInteger(int x)
\{
int i;
for (i = TBITNO -1; i >=0; i--)
\{
putchar(('0');
\}
printf("
");
\}
int leading_zeros(int num)
\{
int msb =\(1(\) TBITNO -1);
int count \(=0\);
/* Iterate over each bit */
for (int i =0; i TBITNO; i++)
\{
/* If leading set bit is found */
if ((num i)\& msb)
\{
/* Terminate the loop */
break;
\}
\}
count++;
return count;
\}
// DO NOT MODIFY ANYTHING ABOVE THIS LINE
int number_of_setbits(int num)
\{
// Write a function to return the number of set bits (1s) in integer num.
/* YOUR CODE GOES HERE */
return 0; // Don't forget to update th
An incomplete C program is given that asks an

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!