Flipping bits in an integer without bitwise operations

喜欢而已 提交于 2019-12-11 15:04:23

问题


I need to flip the bits in an integer from 1 to 0 and 0 to 1. E.g 10010 to 01101 The problem is that in HLSL ps_3_0 there are no binary operators. No ~, <<, >>,... Is there a mathematical way of accomplishing this?


回答1:


You can use the following solution

int inverse(int x)
{
    return 0xFFFFFFFFU - x;
}

otherwise:

int inverse(int x)
{
    return -x - 1; // because -x = ~x + 1, only works in 2's complement
}


来源:https://stackoverflow.com/questions/24107095/flipping-bits-in-an-integer-without-bitwise-operations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!