Update appNew update is available. Click here to update.

Set K Bits

Last Updated: 23 Feb, 2023
Easy
yellow-spark
0/40
Avg time to solve 10 mins
Success Rate 85 %
Share
4 upvotes

Problem Statement

Given a number ‘N’. You have to set bits from 0 to Kth position.

Example: If the Given number is 37 (100101) and ‘K’ = 4, then we need to set the 0,1,2,3 bits and thus after the Kth bit set our answer would be 47 (101111).

Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 10
-10^9 <= N <= 10^9
1 <= K <= 31


Time limit: 1 sec
Sample Input 1:
2
5 2
16 3
Sample Output 1:
7
23
Explanation For Sample Input 1:
In the first test case, we have the number ‘N’ as 5 (101) and K=2. Thus after set 2 bits from the right, the answer will be 7 (111).

In the second test case, we have the number ‘N’ as 16 (10000) and K=3. Thus after set 3 bit from right, the answer will be 23 (10111).
Sample Input 2:
2
10 1
34 4   
Sample Output 2:
11
47
Explanation For Sample Input 2:
In the first test case, we have the number ‘N’ as 10 (1010) and K=1. Thus after set 1 bit from right, the answer will be 11 (1011).

In the second test case, we have the number ‘N’ as 32 (100010) and K=4. Thus after set 3 bit from the right, the answer will be 47 (101111).
Reset Code
Full screen
Autocomplete
copy-code
Console