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...

C Program to Extract n Bits from a Given Bit Position

๐Ÿง  C Program to Extract n Bits from a Given Bit Position

#include <stdio.h>

int main() {
    int num, pos, n;

    // Input number, position, and number of bits
    printf("Enter the number: ");
    scanf("%d", &num);

    printf("Enter the starting bit position (0-based): ");
    scanf("%d", &pos);

    printf("Enter the number of bits to extract: ");
    scanf("%d", &n);

    // Step 1: Right shift to bring desired bits to the end
    int shifted = num >> (pos - n + 1);

    // Step 2: Mask with n 1's => (1 << n) - 1
    int mask = (1 << n) - 1;

    // Step 3: AND operation to get only those n bits
    int result = shifted & mask;

    printf("Extracted %d bits from position %d = %d (binary)\n", n, pos, result);

    return 0;
}
  

๐Ÿ“ Explanation:

This C program extracts n bits from a number starting at a specified bit position pos (0-based from LSB).

  • num >> (pos - n + 1) shifts the desired bits to the end.
  • (1 << n) - 1 creates a bitmask of n 1's.
  • & operation filters out only the required bits.

๐Ÿ’ก Sample Output:

Enter the number: 182
Enter the starting bit position (0-based): 7
Enter the number of bits to extract: 3
Extracted 3 bits from position 7 = 2 (binary)
  

๐Ÿ” Keywords:

bitwise extraction C, extract bits from number in C, shift mask AND example, C programming bit manipulation

๐Ÿ” Extract n Bits from a Number at a Given Position

Example:

  • Number: 202
  • Binary: 11001010
  • Position: 6 (counting from LSB = 0)
  • Bits to extract (n): 3

๐Ÿง  Step-by-Step Visual Diagram:

Bit Index:    7   6   5   4   3   2   1   0
Binary:       1   1   0   0   1   0   1   0
Extracting:       ↑   ↑   ↑
                 pos=6 (3 bits: 6,5,4)

Selected Bits:    1   0   0  → Binary = 4
  

๐Ÿ“Œ Explanation:

  1. Convert the number to binary: 202 → 11001010
  2. Start from position 6, extract 3 bits → positions 6, 5, and 4
  3. Bits at those positions = 1 0 0
  4. Binary 100 = Decimal 4

✅ Final Output: 4

Comments

Popular Posts

๐ŸŒ™