Jump to content

BITAND and the limit of 2147483647


Recommended Posts

Hi there, I'm trying to find a different method (or maybe a way around) to BITAND which has a limit of 2147483647

Im using it in a program that uses the power of 2 to store which accessories an item has. I.E

Accessorie 1 = 1

Accessorie 2 = 2

Accessorie 3 = 4

Accessorie 4 = 8

So with a result of 14 I know its Accessories 2, 3 and 4.

I didn't realise there was a limit and have accessories up to 17592186044416 which is 45 accessories.

Obviously i'm hitting issues as soon as I need to use accessories 32 and up as that's when it hits 2147483648 which is 1 above the limit.

If BitAND($Binary, $AccessoriesTemp[$i][2]) > 0 then
    $AccessoriesTemp[$i][3] = 0
EndIf

This if statement lets everything through if the $binary number is 2147483648 or above, as its stuck with the limit of 2147483647. is there a better method to store data rather than with the power of 2 method, or am I doing something wrong with the BITAND function?

Thanks in advance

Link to comment
Share on other sites

It depends on the goal you want to achieve:

  • Performance
  • The need to know which accessories are used by an item
  • ...

To my mind come Arrays and Dictionaries.

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

When equipment is saved all the accessories that are checked in a listview that are with that equipment will be scanned through and all there values are added together and saved into a sqlite database. When that equipment item Is reopened in the future the total is given and then goes through BITAND to work out which ones were used to make that total. 

I used to use a long conveluted method like this

$Calculation = ""
$Total = $REP_Accessoriestotalread  ;  Binary total
Array = StringSplit($Accessoriesbinary, "|")  ;  List of all possible binary numbers
$i = $Array[0] 
Do
    If Number($Total) >= Number($Array[$i]) Then
        $Total -= $Array[$i]
        If $Calculation = "" Then
            $Calculation &= $Array[$i]
        Else
            $Calculation &= "|" & $Array[$i]
        EndIf
    EndIf
    $i -= 1
Until $i = 0

 

This would just give me string (which I would then split into an array) of all the individual binary numbers which I the would need to do another loop after checking the number against the complete binary array and then entering those details into a listview. was hoping to get away from this as the BITAND work so amazingly with only a few lines of code but didn't realise some of the less used ones with higher numbers messes everything up. do you recon I should go back to my original method if im going to have numbers higher than 2147483647?

Link to comment
Share on other sites

I would fill an array with the data you retrieve from the listview (if item 4 is checked then array element 4 gets set to "x" (or whichever value you prefer)).
Then create a string out of the array using _ArrayToString ("|" or any other character as delimiter) and store this string in your DB.
To fill the listview you read the datafield from your DB, split the string with the selected delimiter into an array. Then loop through the array and fill the listview with the used accessories.
This way you can store a nearly unlimited number of accessories and do not need to change the structure of your DB when you change the number of accessories.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

The only issue I have with that method water is sometime the accessories change name slightly. So if in the future the accessories change slightly the equipment already booked in will have accessories noted that don't exist, with the number method its like its own ID. 

 

Thinking about it I might do what you've said but with an ID number. I've had a program running years at a company that uses the old looping system with a binary number total but I can incorporate something into my program that will scan through the database on first run and convert all the current ones to a string of numbers that can be split up.

 

Thank you both for the help. 

Link to comment
Share on other sites

Using an ID would have been my next suggestion ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

For 64-bit bitwise operations ther's also this:

Now, when storing such things in SQLite, the recommended way is to setup a foreign key to accessories referencing main items.

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

I'd use BitOr to find out which bits are set.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

1 hour ago, BrewManNH said:

I'd use BitOr to find out which bits are set.

@BrewManNH

A bitwise OR is commonly used to set 1 or more bits.  A bitwise AND is commonly used to test if one or more bits are set. 

How exactly would you use a bitwise OR to find out which bits are set?

Link to comment
Share on other sites

Here I made recap of the UDFs compare to native operators.  There is a know issue that won't be resolved with native ^ (power), and of course BITand only work at 32 bits.  Changes must be made to j0kky UDF cause it suffers from the same power issue.  Only JCHD UDF works as expected.

;#AutoIt3Wrapper_UseX64=y
#include "addons.au3"
#include "64 bits Integer.au3"

; known "issue" that won't be fixed : https://www.autoitscript.com/trac/autoit/ticket/3703
; Table of : https://en.wikipedia.org/wiki/Power_of_two

Example1()
Example2()
Example3()

; =======  Native operators ========
Func Example1 ()
ConsoleWrite ("=======  Native operators ========" & @CRLF)
For $i = 45 to 63
  $var1 = 2^$i
  ConsoleWrite ($i & @TAB & VarGetType($var1) & " / " & $var1 & " / " & Int($var1,2) & " / " & BitAND($var1,0xFFFFFFFFFFFFFFFF) & @CRLF)
Next
EndFunc

; =======  JCHD UDF ========
Func Example2 ()
ConsoleWrite ("=======  JCHD UDF ========" & @CRLF)
For $i = 45 to 63
  $hex = hex (2^($i-32),8) & "00000000"
  $uInt1 = Dec($hex,2) ; or _HexToInt64($hex)
  $uInt2 = Dec('FFFFFFFFFFFFFFFF',2) ; or _HexToInt64('FFFFFFFFFFFFFFFF')
  ConsoleWrite ($i & @TAB & $uInt1 & " / " & _BitAnd($uInt1, $uInt2) & @CRLF)
Next
EndFunc

; =======  j0kky UDF ========

Func Example3 ()
ConsoleWrite ("=======  j0kky UDF ========" & @CRLF)
For $i = 45 to 63
  $hex = hex (2^($i-32),8) & "00000000"
  $uInt1 = Dec($hex,2)
  $uInt2 = Dec("FFFFFFFFFFFFFFFF",2)
  ConsoleWrite ($i & @TAB & $uInt1 & " / " & _BitAnd64($uInt1, $uInt2) & @CRLF)
Next
EndFunc

 

Edited by Nine
Link to comment
Share on other sites

Ok corrected j0kky code to work nicely both x86 and x64 ;)

#AutoIt3Wrapper_UseX64=y

For $i = 45 To 63
  $hex = Hex(2 ^ ($i - 32), 8) & "00000000"
  $uInt1 = Dec($hex, 2)
  $uInt2 = Dec("FFFFFFFFFFFFFFFF", 2)
  ConsoleWrite($i & @TAB & $uInt1 & " / " & _BitAND64($uInt1, $uInt2) & @CRLF)
Next

Func _BitAND64($iValue1, $iValue2)
  If Not ((VarGetType($iValue1) = "Int64") Or (VarGetType($iValue2) = "Int64")) Then Return BitAND($iValue1, $iValue2)
  $iValue1 = __DecToBin64($iValue1)
  $iValue2 = __DecToBin64($iValue2)
  Local $aValueANDed[64], $i
  For $i = 0 To 63
    $aValueANDed[$i] = ($iValue1[$i] And $iValue2[$i]) ? 1 : 0
  Next
  Return __BinToDec64($aValueANDed)
EndFunc   ;==>_BitAND64

Func _BitOR64($iValue1, $iValue2)
  If Not ((VarGetType($iValue1) = "Int64") Or (VarGetType($iValue2) = "Int64")) Then Return BitOR($iValue1, $iValue2)
  $iValue1 = __DecToBin64($iValue1)
  $iValue2 = __DecToBin64($iValue2)
  Local $aValueORed[64], $i
  For $i = 0 To 63
    $aValueORed[$i] = ($iValue1[$i] Or $iValue2[$i]) ? 1 : 0
  Next
  Return __BinToDec64($aValueORed)
EndFunc   ;==>_BitOR64

Func _BitXOR64($iValue1, $iValue2)
  If Not ((VarGetType($iValue1) = "Int64") Or (VarGetType($iValue2) = "Int64")) Then Return BitXOR($iValue1, $iValue2)
  $iValue1 = __DecToBin64($iValue1)
  $iValue2 = __DecToBin64($iValue2)
  Local $aValueXORed[64], $i
  For $i = 0 To 63
    $aValueXORed[$i] = (($iValue1[$i] And (Not $iValue2[$i])) Or ((Not $iValue1[$i]) And $iValue2)) ? 1 : 0
  Next
  Return __BinToDec64($aValueXORed)
EndFunc   ;==>_BitXOR64

Func _BitNOT64($iValue)
  If Not (VarGetType($iValue) = "Int64") Then Return BitNOT($iValue)
  $iValue = __DecToBin64($iValue)
  For $i = 0 To 63
    $iValue[$i] = Not $iValue[$i]
  Next
  Return __BinToDec64($iValue)
EndFunc   ;==>_BitNOT64

Func __DecToBin64($iDec)
  Local $tI64 = DllStructCreate("int64 num"), $aBin[64], $iVal
  Local $tI32 = DllStructCreate("align 4;uint low;uint high", DllStructGetPtr($tI64))
  $tI64.num = $iDec
  For $i = 0 To 31
    $iVal = 2 ^ $i
    $aBin[$i] = BitAND($tI32.low, $iVal) ? 1 : 0
    $aBin[$i + 32] = BitAND($tI32.high, $iVal) ? 1 : 0
  Next
  Return $aBin
EndFunc   ;==>__DecToBin64

Func __BinToDec64($aBin)
  Local $tI32 = DllStructCreate("align 4;uint low;uint high"), $iVal
  Local $tI64 = DllStructCreate("UINT64 num", DllStructGetPtr($tI32))
  For $i = 0 To 31
    $iVal = 2 ^ $i
    $tI32.low += $aBin[$i] ? $iVal : 0
    $tI32.high += $aBin[$i + 32] ? $iVal : 0
  Next
  Return $tI64.num
EndFunc   ;==>__BinToDec64

 

Link to comment
Share on other sites

That is brilliant thank you Nine, I will incorporate that into the program and see how it runs. Thank you all for the amount of help you have all provided. Im still a little unsure on exactly how Bit calculations work but with these UDF's I can watch them work and hopefully understand them better.

 

Many thanks for all the help with such small amount of information.

Link to comment
Share on other sites

8 hours ago, TheXman said:

A bitwise OR is commonly used to set 1 or more bits. 

Yeah, you're right, was thinking backwards.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

14 hours ago, Joboy2k said:

Im still a little unsure on exactly how Bit calculations work but with these UDF's I can watch them work and hopefully understand them better.

Hint : there are various videos on Youtube that illustrate bit calculations, for example :

https://www.youtube.com/watch?v=-LNUXmMUMAM

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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...