Sign in to follow this  
Followers 0
piginablanket

Bitshift is returning incorect results

6 posts in this topic

#1 ·  Posted (edited)

Can someone explain why this returns -13002 instead of 52534, like it should be?

BitShift(3442917988, 16)
Edited by piginablanket

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

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

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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)

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
Sign in to follow this  
Followers 0