What happens if I assign a negative value to an unsigned variable?

前端 未结 6 969
旧巷少年郎
旧巷少年郎 2020-11-22 07:31

I was curious to know what would happen if I assign a negative value to an unsigned variable.

The code will look somewhat like this.

unsigned int nVa         


        
6条回答
  •  醉梦人生
    2020-11-22 08:10

    Yes, you're correct. The actual value assigned is something like all bits set except the third. -1 is all bits set (hex: 0xFFFFFFFF), -2 is all bits except the first and so on. What you would see is probably the hex value 0xFFFFFFFB which in decimal corresponds to 4294967291.

提交回复
热议问题