反转32位无符号int位数

时间:2017-01-17 10:04:14

标签: c bit

我正在尝试反转32位的无符号整数,想要将零转换为1,将1转为零,但似乎存在错误。

输入:3

输出:4294967292

预期产量:2147483648

public void ButtonClick()
{
    gameController.AnswerButtonClick (AnswerData.isCorrect);

    if (gameController.IsCorrected())
    {
        GetComponent<Button>().image.color = Color.green;
        Debug.Log("im true");
        // StartCoroutine(ReturnButtonColor());
    }
    else
    {
        GetComponent<Button>().image.color = Color.red;
        // Like this:
        gameController.GetCorrectButton().image.color = Color.green;
    }
}

1 个答案:

答案 0 :(得分:2)

你的期望是错误的。

让我们用十六进制做所有事情:

0x00000003 ^ 0xffffffff = 0xfffffffc

十六进制的正确输出因此为0xfffffffc,其中4294967292为(无符号)十进制。

或二进制:

  00000000000000000000000000000011    0x00000003
^ 11111111111111111111111111111111    0xffffffff
----------------------------------    ----------
  11111111111111111111111111111100    0xfffffffc
相关问题