GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 Re: GML Code Help » gml script test chance dice have no clue » 2009-06-27 18:57:40

So, you're using less than 15 arguments, right?
A bit offtopic, but I would just use something like

Expandresult=choose("apple", "note", "candle", "gold", "empty", "empty", "empty", "empty")

You also can return real objects, but I would replace empty with noone

Cheers

#2 Re: Script Submission » more bit-wise » 2008-09-30 17:34:30

Hi XOT,
thanks for the remarks - it's always a pleasure to listen to a clever man smile

RE>Names... just rename it whatever you want - the meaning and functionality remains.
Personally I would prefer shorter variants.

RE> BiN
I see no problem here but sheer GM limitation: either string or real.

Expandvar b,z,m;
b=argument0
z=""
do
{
    m=b&1>0                          // check the left-most bit
// m=abs(b mod 2)                // alternatative

//_____________________________
    if m>0 z="1"+z else z="0"+z      // add result to reversed string 'z'
//_____________________________
    b=b>>1                           // shift all bits right
// b=b div 2                     // alternatative
}until b=0

return z

It's quite easy to return an integer representing the BINary value, but it would be even more confusing:
- a decimal consisting of 0's and 1's
- much more limited legth --> less flexible --> less useful
- an integer which is not really so integer etc.

Probably Mark wanted to solve 'type casting' for simplification, but it's like 'a division by zero' for me.
I agree with some points thou, but the main purpose was to give the idea.
_____________
Cheers

P.S. Trust NO person and program... especially if you're the author.

#3 Script Submission » more bit-wise » 2008-09-30 08:45:55

DZiW
Replies: 2

Hi people,

ABSTRACT
Recently I wanted to play with flood-fill like code and decided to visit the forum bitwise section...
But there are NO basic scripts for bitwise operation (at the moment) !
So there're 2 possible thoughts: a) either everybody knows it (quite doubtfully) OR b) don't care.
And Mr. XOT said that they may be already posted, but not published online yet. So...

GML NUANCES
1. variable types: string OR real, but latter is represented as integer for bitwise operations
2. ^ - bitwise XOR, not power(a,b)
3. in GM it's easier to implement scripts as 'functions' (d = bit_set(a,b)) than 'procedures' (bit_set(a,b))
...

LOGiC

BiT_GET = A AND (1 SHL B )                     BiT_RESET = A AND (NOT (1 SHL B ))
BiT_SET = A  OR  (1 SHL B )                     BiT_XOR    = A XOR (1 SHL B )

*SHL = SHift Left [<<]
All these operations include (1 SHL B ) part which is a faster version of power(2,B ).

IMPLEMENTATiON

Expand#define bit_get
/*
**  Usage:
**      d = bit_get(b,n)
**
**  Arguments:
**      b       value (as integer)
**      n       bit place, integer
**
**  Returns:
**      d = the nth bit value (0/1)
**
**  GMLscripts.com
*/
var b,n;
b=argument0
n=argument1
return (b&(1<<n))>0
Expand#define bit_set
/*
**  Usage:
**      d = bit_set(b,n)
**
**  Arguments:
**      b       value (as integer)
**      n       bit place, integer
**
**  Returns:
**      d = the value with nth bit set (1)
**
**  GMLscripts.com
*/
var b,n;
b=argument0
n=argument1
return b|(1<<n)
Expand#define bit_reset
/*
**  Usage:
**      d = bit_set(b,n)
**
**  Arguments:
**      b       value (as integer)
**      n       bit place, integer
**
**  Returns:
**      d = the value with nth bit clear (0)
**
**  GMLscripts.com
*/
var b,n;
b=argument0
n=argument1
return b&(~(1<<n))
Expand#define bit_xor
/*
**  Usage:
**      d = bit_set(b,n)
**
**  Arguments:
**      b       value (as integer)
**      n       bit place, integer
**
**  Returns:
**      d = the value with nth bit XORed (inverted)
**
**  GMLscripts.com
*/
var b,n;
b=argument0
n=argument1
return b^(1<<n)

And a BONUS

Expand#define bin
/*
**  Usage:
**      d = bin(b)
**
**  Arguments:
**      b       value (as integer)
**
**  Returns:
**      d = the BINary representation of value (as string)
**
**  Notes:
**      no byte-parity chunk size truncation (per 8 bits)
**      as side-effect string_length(z) returns how many bits are required for the value
**
**  GMLscripts.com
*/
  var b,z,m;
  b=argument0
  z="" 
  do
  {
   m=b&1>0          // check the left-most bit
   z=string(m)+z    // add result to reversed string 'z'
   b=b>>1           // shift all bits right
  }until b=0        

  return z

CONCLUSiON
So much information for an up-to-average GM user, and almost nothing new for the rest...
Ok, although these scripts are quite simple I do believe that they need to be at BiTWiSE section.
And although they are quite exotic for an avarage user on and off they may do come pretty handy.
Also you can try negative numbers, but remember they have all but the highest (most-left) bit inverted.
So, you are welcome to use and improve everything... including the code above.
_______
Cheers

P.S. They are remakes of my old TP7 unit... almost ten years old. Thanks XOT wink

Board footer

Powered by FluxBB