easy +10 pts

Set kth Bit

Flip the kth bit of an integer to 1 using bitwise operations.

Write a function `set_kth_bit(num: int, k: int) -> int` that takes a non-negative integer `num` and a non-negative integer `k`, and returns the integer obtained by setting the `k`th bit of `num` to 1. Bit positions are counted from 0 (the least significant bit). If the bit is already 1, the number remains unchanged. The function should work for any `k` from 0 to 31 (inclusive) and for `num` up to 2^31 - 1.

Constraints

- 0 <= num <= 2^31 - 1 - 0 <= k <= 31 - Use bitwise operations only; do not use string conversion or built-in `bin()`.

Example

```python
>>> set_kth_bit(5, 1)  # 5 is 101, set bit 1 -> 111 = 7
7
>>> set_kth_bit(0, 3)  # 0 -> 1000 = 8
8
>>> set_kth_bit(8, 3)  # bit 3 already set -> 8
8
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about creating a mask with only the kth bit set.
Use the left shift operator to create the mask: 1 << k.
Use the bitwise OR operator to combine the mask with num.
The expression `num | (1 << k)` is the entire solution.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.