Question

THIS PROGRAM HAS TO BE WRITTEN IN C single function to count the number of leading...

THIS PROGRAM HAS TO BE WRITTEN IN C

single function to count the number of leading zeroes in the 32-bit input argument passed to the function (leading zeroes are the zeroes that occur in the most significant bit positions of the number until a bit value of ‘1’ is found).

Homework Answers

Answer #1
#include <stdio.h>

int count_leading_zeros(unsigned int n) {
    int count = 0, c = 0;
    while (n > 0) {
        ++c;
        if (n % 2 == 1) {
            count = c;
        }
        n /= 2;
    }
    return 32-count;
}

int main() {
    unsigned int n = 0xFF;
    printf("Number of leading zeros in 0x%x is %d\n", n, count_leading_zeros(n));
    n = 0xF0000000;
    printf("Number of leading zeros in 0x%x is %d\n", n, count_leading_zeros(n));
    n = 0x00F0F0A3;
    printf("Number of leading zeros in 0x%x is %d\n", n, count_leading_zeros(n));
    return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
a single function (and the include directive to your header file) to perform the endian swap...
a single function (and the include directive to your header file) to perform the endian swap operation of the 32-bit input argument passed to this function. Endian ness refers to how the bytes are stored in memory. In a 32- bit word, there are 4 bytes – B0, B1, B2, and B3. Let us assume that B0 refers to the least significant byte and B3 the most significant byte. The function will swap (exchange) bytes B0 and B3, as well...
C++ code please: Write a program to count the number of positive and the number of...
C++ code please: Write a program to count the number of positive and the number of negative inputs values. The program will stop when a 0 is input. Ex: If the input is -5.5 568 2.332 0 the output is 2 positive number(s) and 1 negative number(s). Ex: If the input is 153.0 0.534 2.2 5.6 46.584 0.015 5 0 the output is 7 positive number(s) and 0 negative number(s).
QUESTION 1 During a single clock tick, how many 64-bit values can be written to an...
QUESTION 1 During a single clock tick, how many 64-bit values can be written to an input of a register file with 8 64-bit registers (i.e. an 8x64 register file)? QUESTION 2 Suppose you are designing a processor that contains a register file with 32 32-bit registers (i.e. a 32x32 register file). What is the minimum number of bits required in order to select which register is being written to? QUESTION 3 If the decimal value, 30, is shifted to...
2. Consider the following program written in C syntax: void swap(int a, int b) {   ...
2. Consider the following program written in C syntax: void swap(int a, int b) {    int temp;    temp = a;    a = b;    b = temp; } void main() {    int value = 2, list[5] = {1, 3, 5, 7, 9};    swap(value, list[0]);    swap(list[0], list[1]);    swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 55000 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
# Description: This program is a function which calculates pi #per Leibniz formula,based on the number...
# Description: This program is a function which calculates pi #per Leibniz formula,based on the number of values passed to it. # Programmer: xxxxx def calculate_pi(n):     total, sign = 0, 1     for i in range(n):         term = 1 / (2 * i + 1)         total += term * sign         sign *= -1     total *= 4     return total n = int(input("How many terms: ")) print(calculate_pi(n)) How many terms: 12 3.058402765927333 HW In Puthon Modify...
Please solve in c++ Split the Number Programming challenge description: You are given a number N...
Please solve in c++ Split the Number Programming challenge description: You are given a number N and a pattern. The pattern consists of lowercase latin letters and one operation "+" or "-". The challenge is to split the number and evaluate it according to this pattern e.g. 1232 ab+cd -> a:1, b:2, c:3, d:2 -> 12+32 -> 44 Input: Your program should read lines from standard input. Each line contains the number and the pattern separated by a single whitespace....
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
(In c code only, not c++) We will simulate the status of 8 LEDs that are...
(In c code only, not c++) We will simulate the status of 8 LEDs that are connected to a microcontroller. Assume that the state of each LED (ON or OFF) is determined by each of the bits (1 or 0) in an 8-bit register (high-speed memory). Declare a char variable called led_reg and initialize it to 0. Assume that the least-significant bit (lsb) controls LED#0 and the most-significant bit (msb) controls LED#7. In the main function, build and present a...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT