C++ numbers add to a negative

后端 未结 2 577
鱼传尺愫
鱼传尺愫 2021-01-22 18:59

So I was just practicing coding a dynamic solution to the Fibonacci sequence which would return the n\'th Fibonacci number and I kept coming across a problem which I can\'t quit

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-22 19:46

    Because of how C stores your int (signed int) in memory, the most significant bit indicates a negative number. So you'll get negative number if you overflow it with large numbers.

    Reference:

    • http://en.wikipedia.org/wiki/Integer_overflow
    • http://en.wikipedia.org/wiki/2%27s_complement

提交回复
热议问题