gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Binary representation

#43549 - jandujar - Tue May 24, 2005 3:28 pm

Hello.

I have a problem, and i need a little help.

I have a this:

define

class x{
public:
int position[10];
};

x positions[100];


but x.position[0..9][0..99] only have 0 o 1 value.

Anybody can explain me how to simplifi my class with binary operators:

position[] = {1,0,1,0,1,0,0,1,1,0} = > {1010100110}

To same memory (because position has 100 fields and positions have 150)


Sorry for my english (i'm spanish)
_________________
http://jandujar.homelinux.com
http://www.dsrobot.com

#43574 - Joat - Tue May 24, 2005 8:52 pm

You have a 10x100 playfield with binary values for each position. That means you have 1000 bits of information.

1000/8 = 125 bytes of data, so you can declare your playfield as an array of 125 bytes (or alternatively 63 halfwords or 32 words, it only changes the math a little bit).

Now each element in the array contains 8 (or 16 or 32) playfield tiles. You can test for a bit being set by if (byte & (1<<bitPosition)), where bitPosition is 0..7 (or 15 or 31 if using a halfword or word).

To actually use your array, you need to first figure out what byte you need, and then what bit within that. The easiest way is to figure out what bit you need, indexing from the start, then breaking that down:

bitFromStart = y*width + x;
byteToIndex = bitFromStart >> 3; // divide by 8
bitWithinByte = bitFromStart & 7; // which bit within this byte
if (mapArray[byteToIndex] & (1 << (bitWithinByte))) {
you stored a 1 earler in this tile position
}

(note: this assumes the array was created with the first bit in the LSB, and so forth, but that's a pretty arbitrary choice. doesn't matter which one you use, just make sure to pick the same way when building your maps, and when using them)

When you want to pack your maps, you can either do it in the same manner as above, or take a building approach, where you start with an empty byte, and shift data in, then write it out every 8 bits.

Hope this helps.
_________________
Joat
http://www.bottledlight.com

#43652 - jandujar - Wed May 25, 2005 12:44 pm

Thank's a lot.

This instruccion help me more:
(1 << (bitWithinByte))



One more thing.

Do you know if "string class" works on ds development? Or I have to build my own.
_________________
http://jandujar.homelinux.com
http://www.dsrobot.com

#43982 - cooky - Sat May 28, 2005 9:48 pm

If you want to do this kind of thing. I.e. make large arrays/structs of single bits into larger bytes n an array style then you should look at the programming part of my website www.ceorron.co.uk

There you will find what i call a flag array.
This class can be set up as any class. Just look at the .h file for the functions they are all prity self explanitory. (but no "[] =" operator)

Basically it does exactly what you just talked about.

If you then wanted to insert that flag array into memory for example then you could use class_name.get_array() to give you the pointer to the array itself (not a duplicate array).

As for string classes they are way too large and clumbersome for hand held development. Most people here limit there string sizes and stick to character arrays. It makes a lot of tasks far simpler as it basically means all the rest of your code deosn't need to be anywhere near as dynamic.

#44510 - jandujar - Thu Jun 02, 2005 12:16 pm

O_O

Thank you.

This class is what I'm searching.

I will use it in "Follarium" (in july)
_________________
http://jandujar.homelinux.com
http://www.dsrobot.com

#44924 - cooky - Mon Jun 06, 2005 9:13 pm

Thanks always nice to be of use to some one, some where. :)
_________________
Rolling a six is unlikely but how do you know if you have never picked up the dice.
www.ceorron.co.uk

#47776 - jandujar - Wed Jul 13, 2005 7:35 am

I will use your library flag_array.

I give you credits on my web-page:

www.polarium-puzzles.com
_________________
http://jandujar.homelinux.com
http://www.dsrobot.com