1267. Set the Nth Bit of a Number

EasyBit ManipulationBit ManipulationBit MaskingOR

Given an integer n and a 0-indexed bit position k, return the value obtained by setting the k-th bit of n to 1. If the bit is already 1, n is unchanged.

Input: A JSON object {"n": <integer>, "k": <bit index>} with 0 <= k <= 40.

Output: Return the integer n with bit k forced to 1.

Examples

Example 1
Input: {"n":5,"k":1}
Output: 7
Explanation: Mask 010 OR 101 = 111 = 7.
Example 2
Input: {"n":8,"k":0}
Output: 9
Explanation: Mask 001 OR 1000 = 1001 = 9.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →