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

Set, Clear, Toggle nth Bit in C

Set, Clear, Toggle nth Bit in C

✅ Set, Clear, and Toggle nth Bit in C

#include <stdio.h>

// Function to set the nth bit (make it 1)
int setBit(int num, int n) {
    return num | (1 << n);
}

// Function to clear the nth bit (make it 0)
int clearBit(int num, int n) {
    return num & ~(1 << n);
}

// Function to toggle the nth bit (flip its value)
int toggleBit(int num, int n) {
    return num ^ (1 << n);
}

int main() {
    int num, n;

    printf("Enter a number: ");
    scanf("%d", &num);

    printf("Enter the bit position to manipulate (0-indexed): ");
    scanf("%d", &n);

    printf("\nOriginal number in binary: ");
    for (int i = 31; i >= 0; i--) {
        printf("%d", (num >> i) & 1);
    }

    printf("\n\nAfter setting %dth bit: %d", n, setBit(num, n));
    printf("\nAfter clearing %dth bit: %d", n, clearBit(num, n));
    printf("\nAfter toggling %dth bit: %d", n, toggleBit(num, n));

    printf("\n");

    return 0;
}
  

๐Ÿ“˜ Explanation:

This program demonstrates bit manipulation in C using bitwise operators. The user provides an integer and a bit position (0-indexed). The program then performs:

  • Set Operation: Uses bitwise OR to ensure the nth bit is 1.
  • Clear Operation: Uses bitwise AND with complement to make the nth bit 0.
  • Toggle Operation: Uses XOR to flip the value of the nth bit.
It also prints the binary representation of the original number using bit shifting.

๐Ÿงพ Sample Output:

Enter a number: 10
Enter the bit position to manipulate (0-indexed): 1

Original number in binary: 00000000000000000000000000001010

After setting 1th bit: 10
After clearing 1th bit: 8
After toggling 1th bit: 8
  

๐Ÿ”‘ Keywords:

Bit manipulation in C, set bit operation, clear bit mask, toggle bit using XOR, left shift, binary number handling, C programming, low-level bit logic

๐Ÿ“Œ Hashtags:

#CProgramming #BitManipulation #SetBit #ClearBit #ToggleBit #BinaryInC #CForBeginners #BitwiseOperations

Comments

Popular Posts

๐ŸŒ™