Can someone explain this?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Can someone explain this?

Post 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.
Image
User avatar
Van-B
Chaos Rift Regular
Chaos Rift Regular
Posts: 125
Joined: Tue Aug 10, 2010 7:17 am
Current Project: iPhone puzzle game
Favorite Gaming Platforms: All - Except Amiga
Programming Language of Choice: DBPro, ObjC++
Location: Scotland

Re: Can someone explain this?

Post 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.
Health, ammo.... and bacon and eggs.
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: Can someone explain this?

Post 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.
Image
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Can someone explain this?

Post 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 :|
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: Can someone explain this?

Post 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
Image
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Can someone explain this?

Post 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.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Can someone explain this?

Post 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 ;)
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Can someone explain this?

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply