easy +10 pts

Clear kth Bit

Turn off the k-th bit of an integer using bitwise operations.

Write a function `clear_kth_bit(n: int, k: int) -> int` that returns the integer obtained by setting the k-th bit of `n` to 0. The bits are indexed from 0 (the least significant bit) up to 31. If the bit is already 0, the result should be `n` unchanged. The input `n` is a non-negative integer that fits in 32 bits, and `k` is an integer from 0 to 31 inclusive. Your implementation must use bitwise operations; Python's built-in integer methods are fine.

Constraints

- 0 ≤ n ≤ 2^31 - 1 - 0 ≤ k ≤ 31 - The function should run in O(1) time.

Example

>>> clear_kth_bit(13, 1)
13  # 1101 → 1101 (bit 1 is already 0)
>>> clear_kth_bit(13, 2)
9   # 1101 → 1001
>>> clear_kth_bit(255, 7)
127 # 11111111 → 01111111
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a bitwise NOT to create a mask that has all bits set except the k-th bit.
Remember that ~0 is all ones in Python's infinite-precision integers, but you can mask it with something like (1 << 32) - 1 if needed.
Combine the mask with `n` using an AND operation.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.