Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

C Program to Toggle All Bits After the Most Significant Bit

C Program to Toggle All Bits After MSB

C Program to Toggle All Bits After the Most Significant Bit

/* * Joseph is learning digital logic subject which will be for his next semester. * He usually tries to solve unit assignment problems before the lecture. * Today he got one tricky question. The problem statement is “A positive integer has been * given as an input. Convert decimal value to binary representation. * Toggle all bits of it after the most significant bit including the most significant bit. * Print the positive integer value after toggling all bits”. Constrains- 1<=N<=100 Example 1: Input : 10 -> Integer Output : 5 -> result- Integer Explanation: Binary representation of 10 is 1010. After toggling the bits(1010), will get 0101 which represents “5”. Hence output will print “5”.*/

A positive integer is given as input. Convert the decimal value to binary, toggle all bits after the most significant bit (including the MSB), and print the resulting positive integer.


#include <stdio.h>

int main()
{
    int n, temp;
    int result = 0;
    int place = 1;

    printf("Enter the value: ");
    scanf("%d", &n);

    temp = n;

    while (temp > 0)
    {
        int bit = temp & 1;

        // Toggle bit
        bit = bit ^ 1;

        result += bit * place;

        place = place << 1;
        temp = temp >> 1;
    }

    printf("Result = %d\n", result);

    return 0;
}
  

πŸ“˜ Explanation:

  • The input number is processed bit by bit using bitwise operators.
  • temp & 1 extracts the last bit of the number.
  • bit ^ 1 toggles the bit (0 → 1, 1 → 0).
  • The toggled bit is added to the result using positional value (place).
  • The number is right-shifted to process the next bit.

This continues until all bits up to the most significant bit are toggled.

🧾 Sample Output:

Enter the value: 10
Result = 5
  

πŸ”‘ Keywords:

C bit manipulation program, toggle bits in C, decimal to binary conversion, bitwise operators in C, output based C programs

πŸ“Œ Hashtags:

#CProgramming #BitManipulation #DigitalLogic #OutputQuestions #Programming #1printf

πŸ” Search Description:

Learn how to toggle all bits of a number after the most significant bit using bitwise operators in C. This program includes explanation, sample output, and a dark-themed layout.

Comments

Popular Posts

πŸŒ™