views:

876

answers:

6

Question: Is there an easy way (library function) to perform a bitwise AND or OR on numbers larger than 32-bit in ActionScript?

From the docs: "Bitwise operators internally manipulate floating-point numbers to change them into 32-bit integers. The exact operation performed depends on the operator, but all bitwise operations evaluate each binary digit (bit) of the 32-bit integer individually to compute a new value."

Bummer...

I can't use the & or | ops - does AS expose a library function to do this for Numbers?

Specifics: I'm porting a bunch of java to flex and the java maintains a bunch of 'long' masks. I know that I can split the Java masks into two ints on the flex side. Since all of my mask manip is localized this won't be too painful. However, I'd like to keep the port as 1-1 as possible.

Any suggestions? Thanks!

A: 

what data type are you using to hold these >32bit values?

Scott Evernden
A: 

Both the values and the masks are of type 'Number'. 53bit, I believe.

tyler
A: 

According to http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_11.html, there are no 64-bit integers (signed or unsigned)...only 32-bit.

The Number type, as you mentioned above, has a 53-bit mantissa, which is too short for you.

I searched for a BigNum FLEX implementation, but couldn't find one.

I'm guessing that you will have to simulate this with either an array of ints or a class with a high and low int.

Good Luck, Randy Stegbauer

Randy Stegbauer
+1  A: 

If you don't mind porting some Javascript, Leemon Baird has written a public-domain Javascript library for handling big integers here:

http://www.leemon.com/crypto/BigInt.html

You won't be able to explicitly use the & and | operators, but you should be able to augment the existing code with bitwiseAnd and bitwiseOr methods.

darronschall
+1  A: 

I think your most straightforward option is to break the masks, and if possible the data being masked, into two pieces. You're butting up against a feature gap, so no point in being tricky if you can help it. And if you don't need real BigNum support, best not to even consider it.

fenomas
A: 

`

public class NumberUtils
{
  public static const MSB_CONV : Number = Math.pow(2, 32);

  public static function bitwiseAND(num1 : Number, num2 : Number) : Number {
    var msb1 : int = num1 / MSB_CONV;
    var msb2 : int = num2 / MSB_CONV;

    return (msb1 & msb2) * MSB_CONV + (num1 & num2);
  }
..OR..shiftRight..
}

`

THF