Jump to content

Bitshift is returning incorect results


Recommended Posts

32-bit integer wrap around?

Edited by DatMCEyeBall

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

BitShift(3442917988, 16) is

3442917988 = 11001101 00110110 11000010 01100100  =  0xCD36C264
SHIFT right by 16
Equals
-13002     = 11111111 11111111 11001101 00110110  =  0xFFFFCD36


BitXOR(BitShift(-13002, 16), 0xFFFF0000) is

-13002     = 11111111 11111111 11001101 00110110  =  0xFFFFCD36
XOR
-65536     = 11111111 11111111 00000000 00000000  =  0xFFFF0000
Equals
52534      = 00000000 00000000 11001101 00110110  =  0x0000CD36
 
A work around when the 32 binary bit is a "1" :-


;ConsoleWrite(BitXOR(BitShift(3442917988, 16), 0xFFFF0000) & @LF) ; Returns 52534

Local $iH = 0xCD36C264 ; 0x4D36C264

ConsoleWrite((BitAND($iH, 0x80000000) ? BitXOR(BitShift($iH, 16), 0xFFFF0000) : BitShift($iH, 16)) & @LF)

 
 
Note the behavior of the 32nd bit when it is a "1" or a "0".

         
Instead of
 
v---- 32nd bit is a "1"                                           v-- C(Hex) is 1100(Binary)
11001101 00110110 11000010 01100100  =  0xCD36C264
as above,


we have:-
BitShift(0x4D36C264, 16) is

v---- 32nd bit is a "0"                                         v-- 4 is 0100
01001101 00110110 11000010 01100100 = 0x4D36C264
SHIFT right by 16
Equals
00000000 00000000 01001101 00110110 = 0x00004D36
|<---------------------->| all zeros here

When the 32bit is a "1", all those above zeros become ones.

That is strange behavior.

Edit: Improved work-around example.

Edited by Malkey
Link to comment
Share on other sites

There is nothing strange here. Docs clearly state that "Bit operations are performed as 32-bit integers". Read "signed 32-bit integers".

So 3442917988 is in fact -852049308.

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)

Link to comment
Share on other sites

It is termed an arithmetic shift which I imagine has some neat applications for mathematical calculations. The alternative, logical shift, is also very useful; but there is no such native function in AutoIt so you have to code your own.

Edited by czardas
Link to comment
Share on other sites

That.

Also see >this post for a 64-bit version of bitwise operations.

Edited by jchd

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)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...