I have four integer values (0 – 255) for an ARGB color map.
Now I want to make a unique float or integer of these four integers. Is it possible to do it like the following?
sum = 4 * 255 + A;
sum += 3 * 255 + R;
sum += 2 * 255 + G;
sum += 1 * 255 + B;
Is the value really unique?
You are trying to do a base convert or something of that sort. Anyway the logic is like in base converting.
4 bytes = 32 bit. So 32 bit unsigned integer would do well.
In this case, you have:
it’s like this:
you have 4 bytes of data, meaning
where X is either 1 or 0 valued bit. You map them like this:
and then all you have to do is to add them, but before that you shift the bits. You shift the
Abits to the left, by 8*3 (to be beyond the limits ofR,GandBbits), then shift theRbits by 8*2, and so on.You end up adding these 32 bit integers:
Where
A,R,G,Bcan be either0or1, and represent as a whole, the 8 bit value of the channel. Then you simply add them, and obtain the result. Or as DarkDust wrote, use not the+operator, but instead the|(bitwise or) operator, since it should be faster in this particular case.