Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 06-09-2012, 10:54 AM   #1
midnightgreen
Member
 
Join Date: Jun 2009
Location: UK
Posts: 94
Default Storing multiple values on an Integer? Bitwise?

As an Integer/Uint is 4 bytes, in theory it should be possible to store 4 different Byte sized values, or 2 Short sized values.
(or even 2 Byte sized and 1 Short)
Same thing should work for uint, with an increased range of positive values.

Not an expert in bitwise operations, but I suspect that this could be achieved with getters/setters, which depending on what value I want to read/write, would shift and then XOR it...


Any Bitwise experts out there that could help out with a bit more specifics?
midnightgreen is offline   Reply With Quote
Old 06-09-2012, 12:38 PM   #2
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,679
Send a message via Skype™ to henke37
Default

The performance costs isn't worth the trouble. And what you need here is binary or, bit masking and bit shifting.
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Old 06-09-2012, 12:58 PM   #3
midnightgreen
Member
 
Join Date: Jun 2009
Location: UK
Posts: 94
Default

well it is a Memory issue, on a large 2D tile map ( 512x512) adding only a few properties to each tile, can be prohibitive Memory wise. Storing several values in one Integer however could cut down on this dramatically.

So I am still looking for an actual solution to this
midnightgreen is offline   Reply With Quote
Old 06-09-2012, 02:14 PM   #4
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,679
Send a message via Skype™ to henke37
Default

Have you tried the flyweight design pattern?
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Old 06-09-2012, 11:29 PM   #5
sparX
!Senior Member
 
Join Date: Jan 2010
Posts: 1,675
Default

It would help if you described exactly what type of data it is you're trying to store.
sparX is offline   Reply With Quote
Old 06-10-2012, 02:06 AM   #6
iamgotenks
Super Saiyan
 
iamgotenks's Avatar
 
Join Date: Nov 2011
Location: New Zealand
Posts: 335
Default

A good example of this is with colours, storing the ARGB value inside a uint.

you can simply breakdown each uint into binary and use only portions of it.

>> and << are bit shift a uint either left or right

ActionScript Code:
//extracting colour components var color:uint = 0xff336699; var a:uint = color >>> 24; var r:uint = color >>> 16 & 0xFF; var g:uint = color >>>  8 & 0xFF; var b:uint = color & 0xFF; //storing colour components var a:uint = 0xff; var r:uint = 0x33; var g:uint = 0x66; var b:uint = 0x99; var color:uint = a << 24 | r << 16 | g << 8 | b;
iamgotenks is offline   Reply With Quote
Old 06-10-2012, 09:58 PM   #7
wvxvw
Holosuit User
 
wvxvw's Avatar
 
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw
Default

This would only make sense if you used something like domain memeory (the Alchemy opcodes), and that only if you could use something other than AS3 to generate the code that uses them. This is because in general property access or a function call are way more expensive that what you can save on having all your data in one large memory chunk.

This approach is more typical for C / system level programming (where, for example, it is not uncommon to have a struct and access its fields by reading from a particular offset in memory, but even there this is rather a bad practice, considered dangerous (error prone) and rarely efficient / too much compiler / device dependant.

There are very few tasks where you could use this technique in AS3, but one possibility is when you need a complex condition, which doesn't exceed 32 options - in which case you can use a combination of "flags" - constants each setting only one particular bit of an 32-bits long integer, so that every combination of such constants would also generate a unique integer corresponding to the number of conditions used.
__________________
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it
wvxvw is offline   Reply With Quote
Old 06-10-2012, 10:32 PM   #8
iamgotenks
Super Saiyan
 
iamgotenks's Avatar
 
Join Date: Nov 2011
Location: New Zealand
Posts: 335
Default

I don't know why people are hating on this practice so hard, if you do anything with bitmapData then you will be using these methods for colours. Also, if you're doing network communication for a game, this would be a good way to decrease the data sent. You can store 32 Boolean true/false's inside one 32bit uint. Do you remember those old games where to save your game you would just write down a code? That is basically super compressed saved data (with a check digit to stop people cheating). Dos and windows uses this method often with their file attributes (I think linux too).

Lets have a look at it in use: (note: these are just values I made up and are not the actual file system values)
ActionScript Code:
const ATTRIB_READ:uint = 1;        // 00001 const ATTRIB_WRITE:uint = 2;      // 00010 const ATTRIB_HIDDEN:uint = 4;     // 00100 const ATTRIB_ARCHIVED:uint = 8// 01000 const ATTRIB_SYSTEM:uint = 16;   // 10000 //only using 5 bits of this uint, we've still got plenty of spare bits for other information var myFileAttrib:uint = ATTRIB_READ | ATTRIB_WRITE | ATTRIB_ARCHIVED; //myFileAttrib = 01011 if (myFileAttrib & ATTRIB_HIDDEN) //it's not hidden because we didn't set it to hidden     trace("file is hidden"); if (myFileAttrib & ATTRIB_READ)     trace("file is readable"); if (myFileAttrib & ATTRIB_WRITE)     trace("file is writeable");

Last edited by iamgotenks; 06-10-2012 at 10:48 PM.
iamgotenks is offline   Reply With Quote
Old 06-11-2012, 03:10 AM   #9
bowljoman
Senior Member
 
Join Date: Jan 2008
Posts: 831
Default

I use it where needed and it is awesome! Occasionally i respond to state changes via dispatcher. Sometimes I dont

ActionScript Code:
package com.brassmonkey.arcade.sess {     import flash.events.EventDispatcher;         [Event(name="add",type="com.brassmonkey.arcade.sess.SessionStateEvent")]     [Event(name="remove",type="com.brassmonkey.arcade.sess.SessionStateEvent")]     public class SessionState extends EventDispatcher     {         public static var dispatcher:SessionState=new SessionState();                 internal var state:uint=0;                 public static const NULL:uint = 0;                 public static const OCCUPIED:uint = 1;                 public static const LOCAL:uint = 1<<1;                 public static const ONLINE:uint = 1<<2;                 public static const GAME:uint = 1<<3;                 public static function clearState():void         {             if(isState(GAME))                 dispatcher.dispatchEvent(new SessionStateEvent(SessionStateEvent.REMOVE,GAME));             if(isState(ONLINE))                 dispatcher.dispatchEvent(new SessionStateEvent(SessionStateEvent.REMOVE,ONLINE));                      if(isState(LOCAL))                 dispatcher.dispatchEvent(new SessionStateEvent(SessionStateEvent.REMOVE,LOCAL));                        if(isState(OCCUPIED))                 dispatcher.dispatchEvent(new SessionStateEvent(SessionStateEvent.REMOVE,OCCUPIED));                           dispatcher.state=NULL;                         dispatcher.dispatchEvent(new SessionStateEvent(SessionStateEvent.ADD,NULL));                        }         /**          * Use  <code>isState(val)</code> when comparing values.          * <p>App states are bitflag values and as such can be compounded,          * for example, it can be both ONLINE and OCCUPIED at the same time.</p>          * @return          *          */           public static function state():uint         {             return dispatcher.state;         }               public static function removeState(val:uint):void         {             if(isState(val))             {                 dispatcher.state = dispatcher.state & (~val);                 dispatcher.dispatchEvent(new SessionStateEvent(SessionStateEvent.REMOVE,val));             }         }                 public static function addState(val:uint):void         {             if(!isState(val))             {                 dispatcher.state |= val;                 dispatcher.dispatchEvent(new SessionStateEvent(SessionStateEvent.ADD,val));             }         }         /**          * Use to check state.          * @param val          * @return          *          */           public static function isState(val:uint):Boolean         {             return  ((dispatcher.state & val) == val);         }           } }

using
ActionScript Code:
if(SessionState.isState(SessionState.ONLINE))                         {                                               displayProfile(proT);                         }
__________________
i = (Andy)this;
bowljoman is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:27 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.