User Tag List

Results 1 to 5 of 5

Thread: Control over the bits within a byte?

  1. #1
    Clicker Multimedia Fusion 2

    Join Date
    May 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Control over the bits within a byte?

    Is there a way to have control over the bits within a byte? All the extensions I can find only go down to the byte level.

    I'm wanting to make a game with lacewing, and it's crucial that I have an easy, compact format for maps.
    Tiles have a lot of options, they can be blank, ground, and have walls on all four sides. Instead of assigning byte values for every different possible variation, it'd be really helpful to be able to have bits within the byte mean whether it has a wall on a certain side or not, etc.
    (Helpful not just for the file format, but for the game logic.)

    It seems like the bit mask object might be able to do it? But I can't figure out how to use the blasted thing.

    I'd really appreciate it if anyone has any ideas on how to accomplish this.

  2. #2
    Clicker Fusion 2.5 Developer

    Join Date
    Jul 2008
    Location
    UK
    Posts
    1,393
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    To store the values:
    MyValue = val( "0b" + str$(A) + str$(B) + str$(C)... )
    Where A, B and C are the values of your bits (so either 0 or 1).

    To get the value of a specific bit:
    If MyValue and (2 pow (NumOfBits - WhichBit)) <> 0,
    then value of bit is 1.
    Where A=1, B=2, C=3...

    eg.
    Walls = val( "0b" + str$(North) + str$(East) + str$(South) + str$(West) )

    If Walls and (2 pow (4 - 3)) <> 0,
    then South = 1


    You can also do other nifty stuff, like toggle a specific bit using "xor".
    Set MyValue to MyValue xor (2 pow (NumOfBits - WhichBit))

  3. #3
    Clicker Multimedia Fusion 2

    Join Date
    May 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, thanks MuddyMole! I had no idea that was even possible, and it works perfectly.

    What does the "and" do in the expression, though?

  4. #4
    Clicker Fusion 2.5 Developer

    Join Date
    Jul 2008
    Location
    UK
    Posts
    1,393
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    "And" is one of the three bitwise operators (along with "or" and "xor") supported by MMF2. Wikipedia describes them far better than I ever could: http://en.wikipedia.org/wiki/Bitwise_operation
    The second "And" example on that page (where it talks about "bit-masking"), is exactly the technique I described above.
    To find what string to use to check a specific bit, you can just say 2^(n-1).
    eg. To test the 3rd bit, you use "MyValue and 2^(3-1)"
    ...Except that the bits are ordered from right to left, which makes things slightly more complicated - that's why I subtracted from the total number of bits.

  5. #5
    Clicker Multimedia Fusion 2 DeveloperiOS Export ModuleSWF Export Module
    Fusion 2.5 (Steam)Fusion 2.5 Developer (Steam)Android Export Module (Steam)HTML5 Export Module (Steam)iOS Export Module (Steam)
    King_Cool's Avatar
    Join Date
    Aug 2008
    Posts
    2,335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lets say i have a single AltValue called Abilities_ , and im going to use it as a '1 dimencional bit array' storing what abilitys my Player Character has aquired.
    Each Bit of Abilities_ represents an ability, if the Bit is OFF the ability is not aquired yet, if the Bit is ON the ability is aquired.

    Visualize it like this:
    ( Bit nr 1 is at the far right )

    0000
    Swiming, Climbing, Jumping, Running

    Lets say that 'Running' is activated by default ( Abilities_ default value is 1 )
    0001
    Swiming, Climbing, Jumping, Running


    ADDING NEW ABILITIES ( USING OR )
    Say your character collides with a powerup, which ahould grant the 'Running', 'Jumping' and 'Swiming' ability.

    Set Abilities_ to 'Abilities_ OR 11'

    ...
    This is what happens

    0001 ( Abilities_ )
    1011 ( eleven )
    =
    1011

    As you see this activates ability ( or Bit ) number 1, 2 and 4.
    If any of the two numbers has a bit turned on, the new number has that bit turned on as well ( else, zero is written in that space ).

    CHECKING ABILITIES ( USING AND )
    Say you have reached a part af the game where 'Climbing' and 'Jumping' is required to proceede.
    You would want to check if those Abilities/ Bits are activated.

    If 'Abilities_ AND 6' = 6, the Climbing and Jumping abilities are activated

    ...
    This is what happens

    1011 ( Abilities_ )
    0110 ( six )
    =
    0010 ( two )

    If both Numbers have 1 in the same place, the new number also gets 1 in that place ( if not 0 is written ).
    You can see from the result of the above operation that Player does not have the Climbing and Jumping ability, since 'Abilities_ AND 6' equals 2 and not 6.

Similar Threads

  1. Turning on/ off Bits
    By King_Cool in forum Multimedia Fusion 2 - Technical Support
    Replies: 5
    Last Post: 18th January 2012, 12:53 AM
  2. Blowfish bits
    By Tricolete in forum Multimedia Fusion 2 - Technical Support
    Replies: 4
    Last Post: 9th September 2010, 03:05 PM
  3. About the Byte Array
    By dragonguy in forum Multimedia Fusion 2 - Technical Support
    Replies: 5
    Last Post: 13th May 2008, 10:09 PM
  4. [BUG]TextThreshold: 0-byte-exe
    By Quinto in forum Extension Development
    Replies: 2
    Last Post: 7th May 2008, 04:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •