GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2008-09-30 08:45:55

DZiW
Member
Registered: 2008-04-01
Posts: 3

more bit-wise

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

Last edited by DZiW (2008-09-30 08:47:13)


Feed your head

Offline

#2 2008-09-30 10:36:49

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: more bit-wise

Re: bit_xor()
I think I'd prefer something like bit_toggle() or bit_flip(). This use of the word "xor" is a little misleading or at least a little vague.


Re: bit_reset()
I'd prefer bit_clear(). To me, "reset" implies knowledge of, and reversion to, the original bit state, which is unknowable.


Re: bin()
Although the name is needlessly vauge, it's an interesting approach to the problem. Not strictly a bitwise operation, which implies working only with bit fields, this is a conversion script that duplicates the functionality of dec_to_bin. Personally, I wouldn't do it this way unless it performed at exceptionally increased speed (which I suppose it may, I have not tested it). My problems with it are the casting of Boolean results as integers while making assumptions of internal values given to Booleans, and further relying on string() to properly make another conversion, assuming the first conversion was correct. It's clever, and it (presumably) works, but it's not as portable or future proof as it could be.


These are just my personal, anal retentive views on programming. I'm interested to hear what others have to say.

EDIT: One these days I'll be able to type "xor" without accidentally typing "xot". smile

Last edited by xot (2008-09-30 10:37:59)


Abusing forum power since 1986.

Offline

#3 2008-09-30 17:34:30

DZiW
Member
Registered: 2008-04-01
Posts: 3

Re: more bit-wise

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.

Last edited by DZiW (2008-10-28 17:36:52)


Feed your head

Offline

Board footer

Powered by FluxBB