# How to turn on a particular bit in a number?

Given a number n and a value k, turn on the k’th bit in n.

Examples:

Input: n = 4, k = 2 Output: 6 Input: n = 3, k = 3 Output: 7 Input: n = 64, k = 4 Output: 72 Input: n = 64, k = 5 Output: 80

The idea is to use bitwise << and | operators. Using expression "(1 << (k – 1))“, we get a number which has all bits unset, except the k’th bit. If we do bitwise | of this expression with n, we get a number which has all bits same as n except the k’th bit which is 1.

Below is the implementation of above idea.

## C++

`// CPP program to turn on a particular bit ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Returns a number that has all bits same as n ` `// except the k'th bit which is made 1 ` `int` `turnOnK(` `int` `n, ` `int` `k) ` `{ ` ` ` `// k must be greater than 0 ` ` ` `if` `(k <= 0) ` ` ` `return` `n; ` ` ` ` ` `// Do | of n with a number with all ` ` ` `// unset bits except the k'th bit ` ` ` `return` `(n | (1 << (k - 1))); ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `int` `k = 2; ` ` ` `cout << turnOnK(n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to turn on a particular ` `// bit ` `class` `GFG { ` ` ` ` ` `// Returns a number that has all ` ` ` `// bits same as n except the k'th ` ` ` `// bit which is made 1 ` ` ` `static` `int` `turnOnK(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` ` ` `// k must be greater than 0 ` ` ` `if` `(k <= ` `0` `) ` ` ` `return` `n; ` ` ` ` ` `// Do | of n with a number with ` ` ` `// all unset bits except the ` ` ` `// k'th bit ` ` ` `return` `(n | (` `1` `<< (k - ` `1` `))); ` ` ` `} ` ` ` ` ` `// Driver program to test above ` ` ` `// function ` ` ` `public` `static` `void` `main(String [] args) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `int` `k = ` `2` `; ` ` ` `System.out.print(turnOnK(n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to turn on a ` `# particular bit ` ` ` `# Returns a number that has all ` `# bits same as n except the k'th ` `# bit which is made 1 ` `def` `turnOnK(n, k): ` ` ` ` ` `# k must be greater than 0 ` ` ` `if` `(k <` `=` `0` `): ` ` ` `return` `n ` ` ` ` ` `# Do | of n with a number ` ` ` `# with all unset bits except ` ` ` `# the k'th bit ` ` ` `return` `(n | (` `1` `<< (k ` `-` `1` `))) ` ` ` `# Driver program to test above ` `# function ` `n ` `=` `4` `k ` `=` `2` `print` `(turnOnK(n, k)) ` ` ` `# This code is contributed by ` `# Smitha ` |

*chevron_right*

*filter_none*

## C#

`// C# program to turn on a particular ` `// bit ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns a number that has all ` ` ` `// bits same as n except the k'th ` ` ` `// bit which is made 1 ` ` ` `static` `int` `turnOnK(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` ` ` `// k must be greater than 0 ` ` ` `if` `(k <= 0) ` ` ` `return` `n; ` ` ` ` ` `// Do | of n with a number ` ` ` `// with all unset bits except ` ` ` `// the k'th bit ` ` ` `return` `(n | (1 << (k - 1))); ` ` ` `} ` ` ` ` ` `// Driver program to test above ` ` ` `// function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4; ` ` ` `int` `k = 2; ` ` ` `Console.Write(turnOnK(n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to turn on a particular bit ` ` ` `// Returns a number that has ` `// all bits same as n except ` `// the k'th bit which is made 1 ` `function` `turnOnK(` `$n` `, ` `$k` `) ` `{ ` ` ` ` ` `// k must be greater than 0 ` ` ` `if` `(` `$k` `<= 0) ` ` ` `return` `$n` `; ` ` ` ` ` `// Do | of n with a number with all ` ` ` `// unset bits except the k'th bit ` ` ` `return` `(` `$n` `| (1 << (` `$k` `- 1))); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 4; ` ` ` `$k` `= 2; ` ` ` `echo` `turnOnK(` `$n` `, ` `$k` `); ` ` ` `// This code is contributed by m_kit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6

## Recommended Posts:

- Write an Efficient Method to Check if a Number is Multiple of 3
- Find the Number Occurring Odd Number of Times
- Write an Efficient C Program to Reverse Bits of a Number
- Count number of bits to be flipped to convert A to B
- Rotate bits of a number
- Compute modulus division by a power-of-2-number
- Find whether a given number is a power of 4 or not
- Turn off the rightmost set bit
- Add 1 to a given number
- Next higher number with same number of set bits
- Program to count number of set bits in an (big) array
- Swap bits in a given number
- Binary representation of a given number
- Check if a number is multiple of 9 using bitwise operators
- How to turn off a particular bit in a number?

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.