Set the K-th bit of a given number Last Updated : 19 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Try it on GfG Practice Given a number n and a value k. From the right, set the kth bit in the binary representation of n. The position of LSB(or last bit) is 0, second last bit is 1 and so on. Also, 0 <= k < x, where x is the number of bits in the binary representation of n.Examples: Input : n = 10, k = 2 Output : 14 (10)10 = (1010)2 Now, set the 2nd bit from right. (14)10 = (1110)2 2nd bit has been set. Input : n = 15, k = 3 Output : 15 3rd bit of 15 is already set. Recommended PracticeSet kth bitTry It! To set any bit we use bitwise OR | operator. As we already know bitwise OR | operator evaluates each bit of the result to 1 if any of the operand's corresponding bit is set (1). In-order to set kth bit of a number we need to shift 1 k times to its left and then perform bitwise OR operation with the number and result of left shift performed just before. In general, (1 << k) | n. C++ // C++ implementation to set the kth bit // of the given number #include <bits/stdc++.h> using namespace std; // function to set the kth bit int setKthBit(int n, int k) { // kth bit of n is being set by this operation return ((1 << k) | n); } // Driver program to test above int main() { int n = 10, k = 2; cout << "Kth bit set number = " << setKthBit(n, k); return 0; } Java // Java implementation to set the kth bit // of the given number class GFG { // function to set the kth bit static int setKthBit(int n, int k) { // kth bit of n is being set by this operation return ((1 << k) | n); } // Driver code public static void main(String arg[]) { int n = 10, k = 2; System.out.print("Kth bit set number = " + setKthBit(n, k)); } } // This code is contributed by Anant Agarwal. Python3 # Python implementation # to set the kth bit # of the given number # function to set # the kth bit def setKthBit(n,k): # kth bit of n is being # set by this operation return ((1 << k) | n) # Driver code n = 10 k = 2 print("Kth bit set number = ", setKthBit(n, k)) # This code is contributed # by Anant Agarwal. C# // C# implementation to set the // kth bit of the given number using System; class GFG { // function to set the kth bit static int setKthBit(int n, int k) { // kth bit of n is being set // by this operation return ((1 << k) | n); } // Driver code public static void Main() { int n = 10, k = 2; Console.Write("Kth bit set number = " + setKthBit(n, k)); } } // This code is contributed by // Smitha Dinesh Semwal. PHP <?php // PHP implementation to // set the kth bit of // the given number // function to set // the kth bit function setKthBit($n, $k) { // kth bit of n is being // set by this operation return ((1 << $k) | $n); } // Driver Code $n = 10; $k = 2; echo "Kth bit set number = ", setKthBit($n, $k); // This code is contributed by m_kit ?> JavaScript <script> // Javascript implementation to set the // kth bit of the given number // function to set the kth bit function setKthBit(n, k) { // kth bit of n is being set // by this operation return ((1 << k) | n); } let n = 10, k = 2; document.write("Kth bit set number = " + setKthBit(n, k)); // This code is contributed by rameshtravel07. </script> Output: Kth bit set number = 14 Time Complexity: O(1) Auxiliary Space: O(1) Set kth bit | DSA Problem Comment More infoAdvertise with us Next Article Set all the bits in given range of a number K kartik Improve Article Tags : Bit Magic DSA Qualcomm Practice Tags : QualcommBit Magic Similar Reads Position of the K-th set bit in a number Given two numbers N and K, The task is to find the index of the K-th set bit in the number from the right. Note: Indexing in the binary representation starts from 0 from the right. For example in the binary number "000011", the first set bit is at index 0 from the right, and the second set bit is at 6 min read Set all the bits in given range of a number Given a non-negative number n and two values l and r. The problem is to set the bits in the range l to r in the binary representation of n, i.e, to unset bits from the rightmost lth bit to the rightmost r-th bit. Constraint: 1 <= l <= r <= number of bits in the binary representation of n.Ex 5 min read Toggling k-th bit of a number For a given number n, if k-th bit is 0, then toggle it to 1 and if it is 1 then, toggle it to 0.Examples : Input : n = 6, k = 1Output : 76 is represented as 110 in binary and has its first bit 0, so toggling it will result in 111 i.e. 7.Input : n = 2, k = 3Output : 62 is represented as 010 in binary 3 min read Toggle all the bits of a number except k-th bit. Given a positive (or unsigned) integer n, write a function to toggle all the bits except k-th bit. Here value of k starts from 0 (zero) and from right. Examples: Input : n = 4294967295, k = 0 Output : 1 The number 4294967295 in 32 bits has all bits set. When we toggle all bits except last bit, we ge 4 min read Count unset bits of a number Given a number n, count unset bits after MSB (Most Significant Bit).Examples : Input : 17 Output : 3 Binary of 17 is 10001 so unset bit is 3 Input : 7 Output : 0 A Simple Solution is to traverse through all bits and count unset bits. C++ // C++ program to count unset bits in an integer #include < 7 min read Unset least significant K bits of a given number Given an integer N, the task is to print the number obtained by unsetting the least significant K bits from N. Examples: Input: N = 200, K=5Output: 192Explanation: (200)10 = (11001000)2 Unsetting least significant K(= 5) bits from the above binary representation, the new number obtained is (11000000 4 min read Like