Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #define TBITNO (int) sizeof(int)*8 int trailing_zeros(int num); int number_of_setbits(int); int leading_zeros(int num); void printbitsofInteger(int x); int main(void) { /* Declare variables to get user
#include#define TBITNO (int) sizeof(int)*8 int trailing_zeros(int num); int number_of_setbits(int); int leading_zeros(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, total number of set bits(1s) is = %d, and unset bits (0s) 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 Exercise 1. 20 points) An incomplete C program is given that asks an integer from the user. Your task is to complete two functions defined below and given in 'pe3.c source file. The function called 'printbitsofInteger' that prints the bits of the stored integer value and the function called "leading_zeros' that returns the number of leading zeros of an integer are given for your reference. See the sample runs below, and don't forget to update your return value in each function. (a) (10 points) Write a C function to return the number of set bits (1s) in a given integer using bitwise operators. (b) (10 points) Write a C function 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. Enter any integer: 256 00000000000000000000000100000000 In 256, total number of set bits(15) is = 1, and unset bits (Os) is = 31 Total number of leading zeros in 256 is = 23. Total number of trailing zeros in 256 is = 8. ./pe3s Enter any integer: 44 00000000000000000000000000101100 In 44, total number of set bits(1s) is = 3, and unset bits (Os) is = 29 Total number of leading zeros in 44 is = 26. Total number of trailing zeros in 44 is = 2. cis-macbook-PIU. Enter any integer: 0 00000000000000000000000000000000 In 8, total number of set bits(1s) is = 0, and unset bits (Os) is = 32 Total number of leading zeros in 0 is = 32. Total number of trailing zeros in 0 is = 32. Dr. Enter any integer: -1 11111111111111111111111111111111 In -1, total number of set bits(1s) is = 32, and unset bits (Os) is = 0 Total number of leading zeros in -1 is = 0. Total number of trailing zeros in -1 is = 0. Luis Enter any integer: -150
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started