Bitwise operations and md4digest function

Alex Tweedly alex at tweedly.net
Wed Nov 23 10:37:32 EST 2005


Alejandro Tejada wrote:

>Hi all,
>
>i've been porting this md4digest function
>from Macromedia Director to Runtime Revolution:
><http://www.isometrik.org/md5/lingo_md4_fastest.txt>
>
>Some differences i had noticed while handling
>numbers are:
>
>Numeric Arrays in Director starts from 0 (zero)
>Numeric Arrays in Runrev starts from 1 (one)
>
>In Director:
>put (24 + 64) / 512 * 16 + 16
>results in 16
>
>In RunRev 
>put (24 + 64) / 512 * 16 + 16
>results in 18.75
>
>To get the same result in RunRev
>i had to rewrite the operation like this:
>put round((24 + 64)/( 512 * 16)) + 16
>
>  
>
Right.
Director is using all integer arithmetic, so 88/512 gives 0, multiply by 
16 is still 0, and add 16 is 16.
Rev uses dynamic typing, so 88/512 gives some small fraction, multiply 
by 16 gives 2.75m and add 16 gives you the 18.75

>More problematic is that bitwise operations
>do not return the same results in
>Director and Runrev... for example
>
>in Director
>put bitAnd(-271733879,-1732584194)
>returns -2004318072 
>
>in RunRev
>put -271733879 bitand -1732584194
>returns 0 (zero)
>
>How could i get the same the result in RunRev as
>this bitAnd operation made in Director?
>
>  
>
The problem here is that Rev's bitand operator is defined to work for 
positive numbers only. From the docs:

> Parameters:
> The number1 and number2 are numbers, or expressions that evaluate to 
> numbers, between zero and 4,294,967,295 (2^32 - 1).

Because your values are "less than 0", it gives wrong answer - they 
appear to be < 0 because the top bit is set - the original code probably 
had them defined as unsigned integers (which Director probably doesn't 
support). You can get the right result with

> function mybitand p1, p2
>   if p1 < 0 then  add 2^32 to p1
>   if p2 < 0 then  add 2^32 to p2
>   return p1 bitand p2
> end mybitand

but you may be able to avoid the need for this depending on how the 
input values here are built-up. If they are the results of other bit 
operators, then in Rev it ought to be possible to keep everything as 
positive values, and then just use the standard bitand operator.

If the code isn't too long, I'd be happy to take a look ....

-- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.362 / Virus Database: 267.13.4/176 - Release Date: 20/11/2005




More information about the use-livecode mailing list