-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path17_killKthBit.go
41 lines (26 loc) · 1.33 KB
/
17_killKthBit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
Challenge: Kill K-th Bit
https://codefights.com/arcade/code-arcade/corner-of-0s-and-1s/b5z4P2r2CGCtf8HCR/description
In order to stop the Mad Coder evil genius you need to decipher the encrypted message he sent to his minions. The message contains several numbers that, when typed into a supercomputer, will launch a missile into the sky blocking out the sun, and making all the people on Earth grumpy and sad.
You figured out that some numbers have a modified single digit in their binary representation. More specifically, in the given number n the kth bit from the right was initially set to 0, but its current value might be different. It's now up to you to write a function that will change the kth bit of n back to 0.
Example
For n = 37 and k = 3, the output should be
killKthBit(n, k) = 33.
3710 = 1001012 ~> 1000012 = 3310.
For n = 37 and k = 4, the output should be
killKthBit(n, k) = 37.
The 4th bit is 0 already (looks like the Mad Coder forgot to encrypt this number), so the answer is still 37.
Input/Output
[time limit] 4000ms (go)
[input] integer n
Guaranteed constraints:
0 ≤ n ≤ 231 - 1.
[input] integer k
The 1-based index of the changed bit (counting from the right).
Guaranteed constraints:
1 ≤ k ≤ 31.
[output] integer
**/
func killKthBit(n int, k int) int {
return n &^ (1 << uint(k-1)) // bit clear the k-1th bit
}