Why do unsigned int x = -1 and int y = ~0 have the same binary representation?

前端 未结 5 1561
感动是毒
感动是毒 2020-12-15 09:54

In the following code segment what will be:

  • the result of function
  • value of x
  • value of y
    {
         unsigned int x=-1;         


        
5条回答
  •  臣服心动
    2020-12-15 10:00

    Its pretty easy. The twos complement representation of -1 is 0xFFFFFFFF. Hence that is what x contains.

    The complement operator (~) flips all the bits. So the complement of 0 is a 32-bit number with all the bits set to 1 or 0xFFFFFFFF.

    Edit: As pointed out int he comments. The answer is not A. If it was then it would be saying 0x7FFFFFFF and 0xFFFFFFFF are the same. They're not. The real answer is C (Assuming MAXUNIT is a typo ;)).

提交回复
热议问题