How many times will the while loop be executed?

后端 未结 2 1370
天命终不由人
天命终不由人 2021-01-13 03:38

I am wondering about how many times this while loop would execute. This is a function that adds two numbers using XOR and AND.

def Add(x, y): 

    # Iterate         


        
2条回答
  •  悲&欢浪女
    2021-01-13 04:01

    There is no fixed answer to how many times the while loop is executed. The while loop is always executed when there is a carry bit from one position to another. Hence you need to know how exactly the numbers look like in binary. But what you can say with certainty is what the max possible number of executions is. It is the length of the bigger number as bits + 1. Why? Because if that's the number that a carry can max occur. Let's take add(1,7) = 8 (001 + 111 = 1000). The carry from the first bit is passed two the second position then to the third and then to the forth. 4 iterations this is equivalent to the length of 7 and that + 1 = 4.

提交回复
热议问题