在翻转整数位,而不按位运算 [英] Flipping bits in an integer without bitwise operations

查看:120
本文介绍了在翻转整数位,而不按位运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在从1到0和0-4的整数例如10010翻转比特为1至01101的问题是,在HLSL ps_3_0没有二元操作。没有〜,<&LT ;, >>,...有没有实现这一点的数学方法

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?

推荐答案

您可以使用下面的解决方案

You can use the following solution

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

否则:

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

这篇关于在翻转整数位,而不按位运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆