Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

Count Non-Set (0) Bits in a 32-bit Integer in c

Count Non-Set Bits in C

๐Ÿงฎ Count Non-Set (0) Bits in a 32-bit Integer

#include <stdio.h>

int main()
{
    unsigned int num;
    int count = 0;

    // Read input from user
    printf("Enter the number : ");
    scanf("%u", &num);

    // Loop through all 32 bits
    for (int i = 0; i < 32; i++)
    {
        if ((num & (1 << i)) == 0)
        {
            count++;
        }
    }

    // Output result
    printf("The count of non set bits is %d\n", count);

    return 0;
}
  

๐Ÿ“˜ Explanation:

๐Ÿ”น The program loops over each bit of a 32-bit unsigned integer using a bitwise AND operation.
๐Ÿ”น If a bit is 0, it increments the `count` of non-set bits.
๐Ÿ”น Works for any unsigned integer in the 32-bit range.

๐Ÿงช Sample Output:

Enter the number : 10
The count of non set bits is 30
    

๐Ÿท️ Keywords:

C program to count 0 bits, count unset bits, bitwise logic, 32-bit integer, technical interview question, C beginner practice

Comments

Popular Posts

๐ŸŒ™