Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Bit Shifting?

Author
Message
Plystire
21
Years of Service
User Offline
Joined: 18th Feb 2003
Location: Staring into the digital ether
Posted: 2nd Aug 2009 08:11
I'm a real nitpick about memory consumption, I admit it.

I have a ton of information in a game I'm making, and the game is sectioned off into "cells". The game needs to store information about these cells.

Here's how I envision it working:
Variable 1 - Consumes 1 bit
Variable 2 - Consumes 1 bit
Variable 3 - Consumes 3 bits
Variable 4 - Consumes 3 bits
Variables 5-7 - Consume 8 bits each
Variables 8 & 9 - Consume 1 bit each

Alright, I'm slightly over 4 bytes there.. I want to constrict all of this into as little memory as possible because there will be potentially many thousands of cells that need their information stored. And I would really not like it if the saved files took up megabytes.


So, what I'm wondering about here... is how would I go about storing this information? According to help files, even the bool data type takes up a byte of memory (which doesn't make sense to me... but whatever)

I suppose I could manually flip each bit on considering each cell is only comprised of 26 bits, but that would be a hassle to accomplish.

Is there a method of bit shifting I could use here to move each variable into position and then simply OR them all together into a single value to be stored in memory?

Some example code would be appreciated if possible.


The one and only,


Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 2nd Aug 2009 08:33
Actually there is a way to do bit packing but I believe you need to use a struct.

http://cplus.about.com/od/learningc/ss/lowlevel_10.htm

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Plystire
21
Years of Service
User Offline
Joined: 18th Feb 2003
Location: Staring into the digital ether
Posted: 2nd Aug 2009 09:12
Hey, thanks, that was pretty informative!

I wouldn't have thought to search for "bit packing" I did a search for "bit shifting" and found what I wanted.

I'm not sure how useful that method of packing would work in this application, though.. these variables are strewn throughout the program and only come together when written/read to/from a file.

But thinking about it now.... it may be cleaner code in the long run to whipe everything I've done and define the cells as an array of a struct type designed that way.


.... gah -- my mind is all messed up now because of the complexity of what I'm trying to make!! I can't even think of how to properly organize my code anymore!!! It took me 4 hours to get a set of stairs properly created and rotated!


Thanks very much for the assistance, though, I'll definitely look into it.


The one and only,


Plystire
21
Years of Service
User Offline
Joined: 18th Feb 2003
Location: Staring into the digital ether
Posted: 2nd Aug 2009 09:53
Correct me here if my math is wrong, but this is what I came up with trying to determine worst case scenario of storage for this game.

Let's say I made a cube that is made up of the cells within this game. This cube is 256x256x256 cells in size.

From my original post... each cell will consume 5 bytes of data.

256x256x256 = 16777216 cells
16777216 * 5 = 83886080 bytes
83886080 / 1024 / 1024 = 80 MegaBytes

80 MEGABYTES to store a cube of size 256?!? Is that correct?? Please tell me I messed up someone in there


The one and only,


Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 2nd Aug 2009 11:29
Looks right to me You may need to lower the resolution of the cells.

Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 2nd Aug 2009 11:45
I think your maths is correct. Boolean type is one byte because it is faster that way. I would argue that there is little point in converting everything to be so memory efficient; its not worth the hassle. Instead you could just use a compression library to compress saved files; it will do all this hard work for you.

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 2nd Aug 2009 20:48
The boolean type is 1 byte, because the smallest chunk of memory you can reference using a pointer is 1 byte. To keep a reference to a single bit, you would need a second value indicating the index of the bit inside the byte, and that would mess up everything

Login to post a reply

Server time is: 2024-10-01 08:41:02
Your offset time is: 2024-10-01 08:41:02