Page 1 of 1

Can someone explain this?

Posted: Thu Sep 30, 2010 6:46 am
by Kyosaur
I'm having a little trouble understanding this line of code:

Code: Select all

var = (16777216 * data[0]) + (65536 * data[1]) + (256 * data[2]) + data[3];
What is its use? I get that all of these numbers are powers of twos, with the power increasing by 8 every time. I just dont get what it is used for, can someone please explain it in as much detail as possible?

I have a good understanding of binary, i just cant understand this; even when i convert it to work with shifts and xor.

Re: Can someone explain this?

Posted: Thu Sep 30, 2010 7:02 am
by Van-B
It could be a 24-bit integer converter - sometimes this is used when specifying image colours, RGBA. That could take the 4 byte size components of colour... red, green, blue, and alpha - then store them in a double word integer.

Re: Can someone explain this?

Posted: Thu Sep 30, 2010 8:45 am
by Kyosaur
Van-B wrote:It could be a 24-bit integer converter - sometimes this is used when specifying image colours, RGBA. That could take the 4 byte size components of colour... red, green, blue, and alpha - then store them in a double word integer.
This actually helped me a lot. I just read an article regarding color depth and completely get it now.


Thanks :D.

Re: Can someone explain this?

Posted: Thu Sep 30, 2010 9:54 am
by Ginto8
Van-B wrote:It could be a 24-bit integer converter - sometimes this is used when specifying image colours, RGBA. That could take the 4 byte size components of colour... red, green, blue, and alpha - then store them in a double word integer.
yeah... but it's not exactly the best idea... it should be shifts and OR's :|

Re: Can someone explain this?

Posted: Thu Sep 30, 2010 10:06 am
by Kyosaur
Ginto8 wrote:
Van-B wrote:It could be a 24-bit integer converter - sometimes this is used when specifying image colours, RGBA. That could take the 4 byte size components of colour... red, green, blue, and alpha - then store them in a double word integer.
yeah... but it's not exactly the best idea... it should be shifts and OR's :|

Well this isnt for C(++) so thats not always the case. I made 3 different versions and ran a benchmark on them.

Here are the results:

Code: Select all

#define RGBAToInt1(%0,%1,%2,%3) (((%0) << 24) | ((%1) << 16) | ((%2) << 8 | (%3)))
#define RGBAToInt2(%0,%1,%2,%3) ((16777216 * (%0)) + (65536 * (%1)) + (256 * (%2)) + (%3))
#define RGBAToInt3(%0,%1,%2,%3) ((16777216 * (%0)) | (65536 * (%1)) | (256 * (%2)) | (%3))


//10,000,000 loopings

RGBAToInt (1): Number = 946968831 || Time = 3006 ms
RGBAToInt (2): Number = 946968831 || Time = 2762 ms
RGBAToInt (3): Number = 946968831 || Time = 2894 ms

RGBAToInt (1): Number = -2070156545 || Time = 3038 ms
RGBAToInt (2): Number = -2070156545 || Time = 2764 ms
RGBAToInt (3): Number = -2070156545 || Time = 2897 ms

RGBAToInt (1): Number = 1266655999 || Time = 3029 ms
RGBAToInt (2): Number = 1266655999 || Time = 2764 ms
RGBAToInt (3): Number = 1266655999 || Time = 2900 ms

Re: Can someone explain this?

Posted: Thu Sep 30, 2010 1:10 pm
by qpHalcy0n
Ginto8 wrote:
Van-B wrote:It could be a 24-bit integer converter - sometimes this is used when specifying image colours, RGBA. That could take the 4 byte size components of colour... red, green, blue, and alpha - then store them in a double word integer.
yeah... but it's not exactly the best idea... it should be shifts and OR's :|
Furthermore any decent compiler (MSVC ;)) will turn power of two integer arithmetic into shifts.

Re: Can someone explain this?

Posted: Thu Sep 30, 2010 1:42 pm
by GroundUpEngine
qpHalcy0n wrote:
Ginto8 wrote:
Van-B wrote:It could be a 24-bit integer converter - sometimes this is used when specifying image colours, RGBA. That could take the 4 byte size components of colour... red, green, blue, and alpha - then store them in a double word integer.
yeah... but it's not exactly the best idea... it should be shifts and OR's :|
Furthermore any decent compiler (MSVC ;)) will turn power of two integer arithmetic into shifts.
Says the MSVC fanboy, aha jk ;)

Re: Can someone explain this?

Posted: Thu Sep 30, 2010 2:22 pm
by Ginto8
Kyosaur wrote:Here are the results:
qpHalcy0n wrote:Furthermore any decent compiler (MSVC ;)) will turn power of two integer arithmetic into shifts.
#1: I'm pretty sure g++ does that (but since I have minimal knowledge about compilers, don't quote me)
#2: Yes, multiplication & addition may actually be faster... BUT shifting and ORing can make it much clearer as to what is being done. Also, if you add in some bit masking, shifting and ORing prevents possible overflows that could cause some issues.