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)= (1010)_{10}_{2}Now, set the 2nd bit from right.(14)= (1_{10}110)_{2}2nd bit has been set. Input : n = 15, k = 3 Output : 15 3rd bit of 15 is already set.

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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Kth bit set number = 14

This article is contributed by **Ayush Jauhari**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count number of trailing zeros in Binary representation of a number using Bitset
- Find the largest number smaller than integer N with maximum number of set bits
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Number of ways to split a binary number such that every part is divisible by 2
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Find a number which give minimum sum when XOR with every number of array of integers
- Number of leading zeros in binary representation of a given number
- Minimum number of given powers of 2 required to represent a number
- Maximum number of splits of a binary number
- Program to Convert BCD number into Decimal number
- Check if a number has same number of set and unset bits
- Find the Number Occurring Odd Number of Times
- Number of pairs with Bitwise OR as Odd number
- Find third number such that sum of all three number becomes prime
- M-th smallest number having k number of set bits.
- Minimum number using set bits of a given number
- Next higher number with same number of set bits
- Number of integers with odd number of set bits
- Number whose XOR sum with given array is a given number k
- Add 1 to a given number