Programming Question (Bitwise OR)

Started by AGP, December 28, 2020, 09:08:48 PM

Previous topic - Next topic

AGP

Does it make any difference to bitwise OR 4278190080u and -16777216 (for alpha values) if the result will nevertheless be an unsigned int (I apologize in advance for the C#)?

EgonOlsen

Depends on the implementation, I guess. Just write a simple test case and try it. In case of doubt, I would go with a bitwise or hex representation of the number instead like 0b11111111111.... or 0xfffffff... if that's supported by C#.

AGP

Tell me if you think that the numbers matching here is sufficient proof that it's the same thing (the ifs are there just in case):

int ALPHA = 255 << 24;
uint r = 1, g = 1, b = 2;
if ((r & 0xffffff00) != 0) {
r = (255u >> (int)(r >> 28 & 8));
}
if ((g & 0xffffff00) != 0) {
g = (255u >> (int)(g >> 28 & 8));
}
if ((b & 0xffffff00) != 0) {
b = (255u >> (int)(b >> 28 & 8));
}
uint fromSigned = (uint)(b | (g << 8) | (r << 16) | ALPHA);
uint fromUnsigned = b | (g << 8) | (r << 16) |4278190080u;
System.Console.WriteLine("OR from Signed: "+fromSigned +", from Unsigned: "+fromUnsigned);

EgonOlsen

If these are giving the same results, it should be fine.