Author Topic: Programming Question (Bitwise OR)  (Read 5954 times)

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Programming Question (Bitwise OR)
« on: December 28, 2020, 09:08:48 pm »
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#)?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Programming Question (Bitwise OR)
« Reply #1 on: December 29, 2020, 04:29:24 pm »
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#.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Programming Question (Bitwise OR)
« Reply #2 on: December 29, 2020, 09:31:54 pm »
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):
Code: [Select]
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);

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Programming Question (Bitwise OR)
« Reply #3 on: December 30, 2020, 11:00:18 am »
If these are giving the same results, it should be fine.