15. Write a program in C to toggle a particular bit in a number using bitwise XOR.
To toggle a bit means to set a bit to its complementary state. This means that if a bit is currently set, change it to unset and vice versa.
To make a change or toggle a bit we will use the bitwise XOR operator ^. The bitwise XOR operator returns 1 if the bits corresponding to both operands are different, otherwise it returns 0. We will use this feature of the bitwise XOR operator to swap or toggle bits. For example, if the least significant bit of a number num is 1, then num^1 will make the least significant bit (LSB)of the number 0. And if the least significant bit of the number is 0, then num^1 will make the least significant bit part of the number to 1.
logic to toggle nth bit of any given number.
1. Input any number and a nth bit position to toggle from user.
Store it in some variable say num1 and n1.
2. Left shift 1 to n 1 times, i.e. 1 << n1.
3. Perform bitwise XOR with num1 and result evaluated above i.e. num1 ^ (1 << n1);
#include <stdio.h>
/**
* C program to toggle nth bit of a number
*/
int main()
{
int num1, n1, newNumber;
/* Input number from user */
printf("\nEnter any number: ");
scanf("%d", &num1);
/* Input bit position you want to toggle */
printf("\nEnter nth bit to toggle (0-31): ");
scanf("%d", &n1);
/*
* Left shifts 1, n1 times
* then perform bitwise XOR with num1
*/
newNumber = num 1^ (1 << n1);
printf("\nBit toggled successfully.\n");
printf("\nNumber before toggling %d bit is : %d (in decimal)\n", n1, num1);
printf("\n Number after toggling %d bit is: %d (in decimal)\n", n1, newNumber);
return 0;
}
Output:
Enter any number: 22
Enter nth bit to toggle (0-31): 1
Bit toggled successfully.
Number before toggling 1 bit is : 22 (in decimal)
Number after toggling 1 bit is : 20 (in decimal)
1.Write a program to add two numbers using the '+' operator.
2.Write a program in C to subtract two numbers using the '-' operator.
3.Write a program in C to multiply two numbers using the '*' operator.
4.Write a program to divide two numbers using the '/' operator.
5.Write a program in C to find the remainder of two numbers using the '%' operator.
6.Implement a program in C to perform bitwise AND on two numbers using the '&' operator.
7.Write a program in C to perform bitwise OR on two numbers using the '|' operator.
8.Write a program to perform bitwise XOR on two numbers using the '^' operator.
9.Write a program in C to perform bitwise left shift on a number using the '<<' operator.
10.Implement a program in C to perform bitwise right shift on a number using the '>>' operator.
11.Write a program to in C check if a number is even or odd using the bitwise AND operator.
12.Write a program in C to swap two numbers without using a temporary variable.
13.Write a program in C to find the minimum of two numbers without using the 'if' statement.
14. Write a program in C to check if a number is negative without using the 'if' statement.
Other Topic:-->>Nested While Loop. || conditional statements Assignments in C.