Find, Set, Clear, Toggle and Modify bits in C - GeeksforGeeks

11 Dec, 2020
Given a positive integer N, the task is to perform the following sequence of operations on the binary representation of N in C.
Finding a bit: Find the Kth bit in binary representation of N.
Setting a bit: If Kth bit is 0, then set it to 1. Otherwise, leave it unchanged.
Clearing a bit: If Kth bit is 1, then clear it to 0. Otherwise, leave it unchanged.
Toggling a bit: If Kth bit is 1, then change it to 0 and vice-versa.
Modifying a bit: Replace the Kth bit with a given bit.
Examples:
Input:N = 5, K = 1, P = 0Output:K(= 1)th bit of 5 is 1.Setting the K(= 1)th bit modifies N to 5Clearing the K(= 1)th bit modifies N to 4.Toggling the K(= 1)th bit modifies N to 4.Replacing the K(= 1)th bit with P(= 0) modifies N to 4
Input: N = 10, K = 2, P = 1Output:Kth(= 2) bit of 5 is 1.Setting the K(= 2)th bit modifies N to 10.Clearing the K(= 2)th bit modifies N to 8.Toggling the K(= 2)th bit modifies N to 8.Replacing the K(= 2)th bit with P(= 1) modifies N to 10.
Approach: Follow the steps below to find, set, clear, toggle and modify the Kth bit in the binary representation of N.
Finding a bit:
 (N >> K) & 1
Setting a bit:
 N = N | (1 << K)
Clearing a bit:
 N = N & ~(1 << K)
Toggle a bit:
 N = N ^ (1 << K)
Modify a bit:
 N = N | (P << K)
Below is the implementation of the above approach:
C
// C program to implement// the above approach  #include <stdio.h>  // Function to set the kth bit of nint setBit(int n, int k){    return (n | (1 << (k - 1)));}  // Function to clear the kth bit of nint clearBit(int n, int k){    return (n & (~(1 << (k - 1))));}  // Function to toggle the kth bit of nint toggleBit(int n, int k){    return (n ^ (1 << (k - 1)));}  // Function to modify k-th bit with pint modifyBit(int n, int k, int p){    return (n | (p << k));}  // Function to find the kth bit of nint findBit(int n, int k){    return ((n >> k) & 1);}  // Utility function to perform// the specified Bit Operationsvoid bitOperations(int n, int k,                   int p){      printf("K(= %d)-th bit of %d is %d\n",           k, n, findBit(n, k));      printf("Setting K(= %d)th bit modifies N to %d\n",           k, setBit(n, k));      printf("Clearing K(= %d)th bit modifies N to %d\n",           k, clearBit(n, k));      printf("Toggling K(= %d)th bit modifies N to %d\n",           k, toggleBit(n, k));      printf("Replacing the K(= %d)<sup>th</sup> bit", k);    printf(" with P(= %d) modifies N to 10\n",           modifyBit(n, k, p));}  // Driver Codeint main(){    int n = 5, k = 1, p = 1;    bitOperations(n, k, p);      return 0;}
K(= 1)-th bit of 5 is 0
Setting K(= 1)th bit modifies N to 5
Clearing K(= 1)th bit modifies N to 4
Toggling K(= 1)th bit modifies N to 4
Replacing the K(= 1)th bit with P(= 7) modifies N to 10

Time Complexity: O(log(N))Auxiliary Space: O(1)
Bit Algorithms
Bitwise-AND
Bitwise-OR
Bitwise-XOR
Technical Scripter 2020
Bit Magic
Mathematical
Technical Scripter
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Write an Efficient Method to Check if a Number is Multiple of 3
Swap two nibbles in a byte
Highest power of 2 less than or equal to given number
Swap bits in a given number
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7