TigBits

C++ to AutoIt: Problem with Bitwise Operations

3 posts in this topic

#1 ·  Posted (edited)

I am trying to replicate this C++ code in AutoIt.

#include <stdio.h>
#include <stdint.h>
     
int main() {
    uint32_t k;
    uint32_t table[256];
     
    k = 1440027834;
    k ^= 0x55555555;
     
    printf("%u\n", k);
     
    for (unsigned i = 0; i < 256; i++) {
        k = (k >> 1) | (k << 31);
        k *= 39916801;
        table[i] = k;
     
        printf("%u\n", k);
    }
    return 0;
}

This is my AutoIt conversion.

#include <Array.au3>

_Main()

Func _Main()
    Local $i_K = 1440027834
    $i_K = BitXOR($i_K, 0x55555555)
    ConsoleWrite($i_K & @CRLF)

    Local $a_Table[256]
    For $i = 0 To 255
        $i_K = BitOR(BitShift($i_K, 1), BitShift($i_K, -31))
        $i_K *= 39916801
        $a_Table[$i] = $i_K
    Next
    _ArrayDisplay($a_Table)
EndFunc

The initial BitXOR works fine, but something is going wrong with the BitOR and\or BitShifts. AutoIt is giving a value of -85552917371984905 for $a_Table[1] while C++ gives a value of 780887031. I know that in the past there have been problems with Bitshifts internal workings but is that the problem here? What can I do to get the correct output?

Here's a link for the C++ code and its console output.

http://ideone.com/skxZZ8

EDIT: After comparing the values of the Bitshifts individually, the problem appears to originate from the BitOR. The values mismatch on the fifth loop iteration.

Edited by TigBits

Share this post


Link to post
Share on other sites



This is because AutoIt expands $i_K to 64-bit signed integer, but if you keep only the last dword the result is correct:

  780887031 = 0x2E8B63F7
-85552917371984905  = 0xFED00E132E8B63F7

Change the multiply to

$i_K = BitAND($i_K * 39916801, 0xFFFFFFFF)

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
3 hours ago, jchd said:

This is because AutoIt expands $i_K to 64-bit signed integer, but if you keep only the last dword the result is correct:

  780887031 = 0x2E8B63F7
-85552917371984905  = 0xFED00E132E8B63F7

Change the multiply to

$i_K = BitAND($i_K * 39916801, 0xFFFFFFFF)

 

 

I see, thank you very much, jchd.

With this change the code gets to the fifteenth loop iteration before a mismatch occurs. The mismatch is caused by the first BitShift and is fixed by using wraithdu's _BitShift() function. Now the AutoIt output matches the C++ output for the whole table.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now