Isn’t the following solution simpler and of the same time and space complexity?
def calculate_bitwise_complement(n):
result = 0
current_bit = 1
while current_bit <= n:
if (n & current_bit) == 0:
result += current_bit
current_bit = current_bit << 1
return result