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 'numberofsetbits' and 'trailingzeros' defined below and given in pec source file.
Note that there are two other functions 'printbitsofInteger' and 'leadingzeros' given for your reference. The function called 'printbitsofInteger' prints the bits of the stored integer value and the function called 'leadingzeros' 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 : Copy the given file pec to your working directory.
Step : Open the given incomplete C source code named as pec with vim by using the command: vim pec
a Write a C function numberofsetbits' to return the number of set bits s in a given integer using bitwise operators.
b Write a C function trailingzeros' 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 pec but they are incomplete. You are expected to complete them.
Step : Build your executable program named as pe by using the command: gcc o pec
Step : Run the program by using the command: pe
Step : Test your code with the provided "testsh file by using the command: testsh pec
Note : You should have the "testsh in the same directory as your source code.
Note : You should see "PASS" for all the test cases.
Step : Submit your source code pec to Learn.Hub assignment PE
Sample Result
ZafersMacBookPro:src zafer$ pes
Enter any integer:
In total number of set bitss is and unset bits s is
Total number of leading zeros in is
Total number of trailing zeros in is
ZafersMacBookPro:src zafer$ pes
Enter any integer:
In total number of set bitss is and unset bits s is
Total number of leading zeros in is
Total number of trailing zeros in is
ZafersMacBookPro:src zafer$ pes
Enter any integer:
In total number of set bitss is and unset bits s is
Total number of leading zeros in is
Total number of trailing zeros in is
ZafersMacBookPro:src zafer$ pes
Enter any integer:
In total number of set bitss is and unset bits s is
Total number of leading zeros in is
Total number of trailing zeros in is
ZafersMacBookPro:src zafer$ pes
Enter any integer:
In total number of set bitss is and unset bits s is
Total number of leading zeros in is
Total number of trailing zeros in is
DO NOT MODIFY ANYTHING OTHER THAN THE TWO
FUNCTIONS numberofsetbits and trailingzeros
NOTE THAT THE TWO FUNCTIONS THAT YOU WILL
IMPLEMENT ARE AT THE END OF THIS FILE
#include
#define TBITNO intsizeofint
int trailingzerosint num;
int numberofsetbitsint;
int leadingzrosint num;
void printbitsofIntegerint x;
int mainvoid
Declare variables to get user input
int num, ones, count ;
Get user input from the keyboard.
printfEnter any integer: ;
scanfd #
Print bits of the stored integer
printbitsofIntegernum;
Compute number of set bits in num
ones numberofsetbitsnum;
printfIn d ttal number of set bitss is
d and unset bits Os is d
num, ones,
TBITNO ones;
printfTotal number of leading zeros in d is
d
num, leadingzerosnum;
printfTotal number of trailing zeros in d is
d
num, trailingzerosnum;
return ;
void printbitsofIntegerint x
int i;
for i TBITNO ; i ; i
x & i putchar :
putchar;
printf
;
int leadingzerosint num
:
printfTotal number of trailing zeros in d is d
num, trailingzerosnum;
return ;
void printbitsofIntegerint x
int i;
for i TBITNO ; i ; i
putchar;
printf
;
int leadingzerosint num
int msb TBITNO ;
int count ;
Iterate over each bit
for int i ; 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 numberofsetbitsint num
Write a function to return the number of set bits s in integer num.
YOUR CODE GOES HERE
return ; Don't forget to update th
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
