Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

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

๐ŸŒ™