Aaron Benzel
Aaron Benzel

Reputation: 311

Flipping bits in an integer without bitwise operations

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?

Upvotes: 1

Views: 708

Answers (1)

phuclv
phuclv

Reputation: 41754

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
}

Upvotes: 3

Related Questions