MODULE: bit manips v0.4 - bit manipulation functions

Started by ZH, Wed 30/12/2009 08:39:25

Previous topic - Next topic

ZH

The bit manips module includes bit manipulation functions (getting bits, changing bits, and copying sequences of bits) for char, short, and int.

This module is written for AGS 3.1.

The only documentation that should be necessary is in the comments of the script header file.

This module does not conform to the Script Module Guidelines; if its deviation bothers you, you can change it.

Quote from: ZH, 2009/Dec/30
I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
In case this is not legally possible:
 I grant anyone the right to use this work for any purpose, without any conditions, unless such conditions are required by law.

Changes:

  • v. 0.4:
    • made more speed optimizations
  • v. 0.3:
    • added the function construct_byte
    • made various speed optimizations
  • v. 0.2:
    • fixed the functions that copy sequences of bits
  • v. 0.1:
    • first version




Download (Add ".zip" to the end of the filename.)

Dualnames

I'm prolly stupid, but apart from bit manipulation, what does that module REALLY does? I mean where would I need it?
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Khris

I've started creating a tile map editor, and the tile map format exported by ProMotion uses bits to store stuff like whether the tile is flipped, etc. so I had to write those functions myself.
That's just one possible use for that module and no, it doesn't seem to do anything except what the description says. Weird.

ZH

Quote from: Dualnames on Wed 30/12/2009 13:32:08I mean where would I need it?
You never really need it, but it makes getting or changing specific bits easier.
Edit: To be more specific, it's useful mostly for

  • moving sequences of bits across byte boundaries
  • copying some bits but not others
Quote from: Khris on Wed 30/12/2009 15:29:18
I've started creating a tile map editor, and the tile map format exported by ProMotion uses bits to store stuff like whether the tile is flipped, etc. so I had to write those functions myself.
That's just one possible use for that module
Yes.
Quote from: Khris
Weird.
How so?

Charity

Related question: Is using bits in a char or int to store a series of binary values any faster or more space efficient than using multiple bools?

For instance, if one were to use a large array of structs and each stuct needed to hold a large number of binary values (say the presence or not of a plethora of individual status ailments on RPG characters), is there a chance that storing all these values in one int per struct instead of several bools would notably improve performance or filesize?

ZH

Quote from: Lyaer on Fri 01/01/2010 09:49:41Is using bits in a char or int to store a series of binary values any faster

No, it's definitely slower. Additionally, as of version 0.3, certain short and int functions are slower than the corresponding char function, due to slight optimizations I made to the char one but not to the others. I'll probably do the same with those for the next release.

Quote from: Lyaer on Fri 01/01/2010 09:49:41or more space efficient than using multiple bools?

Yes. I don't know the details of AGS's implementation, but boolean types usually take one byte; some compilers (for example, pre-.NET Visual Basic) use two bytes per boolean. You can use as many as eight bits on a char, sixteen bits on a short, or thirty-two bits on an int. If you have to store very many boolean values, storing eight per byte rather than one per byte would reduce memory consumption. The number of boolean values you have to store would have to be very large to overcome the overhead of writing and reading the bits, though.
Edit: I don't remember why I thought the stricken-out part. If you have more than a few status effects, it would reduce memory consumption.

Quote from: Lyaer on Fri 01/01/2010 09:49:41is there a chance that storing all these values in one int per struct instead of several bools would notably improve ... filesize?

Probably, but to be certain I would have to know how the AGS savegame format works.

Charity

Awesome.  Good to know.

I went ahead and changed the formatting to fit the AGS module guidelines.  As far as I know I didn't break anything.  Still compiles at any rate.

http://www.mediafire.com/file/z4rnnmznmmy/BitManips_0_4_1.zip

SMF spam blocked by CleanTalk