BASIC allocates a variable that takes only 0 or 1 to a regular variable. This is inefficient when we use a big-size array.
Download BITARRAY.zip, extract BitArray.DLL and put it into the folder where BASIC.EXE exists to enable to use a huge bit array as follows.
REM Eratosthenes' sieve@
DECLARE EXTERNAL FUNCTION GetArray, Test
DECLARE EXTERNAL SUB FreeArray, setbit
LET Nmax=10000
LET p=GetArray(Nmax)
IF p<>0 THEN
FOR i=2 TO Nmax-1
IF Test(p,i)=0 THEN
PRINT i
FOR j=i^2 TO Nmax-1 STEP i
CALL SetBit(p,j)
NEXT j
END IF
NEXT i
CALL FreeArray(p)
END IF
END
EXTERNAL FUNCTION GetArray(s)
! Gets (s+1) bit memories and clear them with zeros.
! The result is the address of the memory.
! If the result is 0, the function failed.
FUNCTION GetArray_sub(s)
ASSIGN "BitArray.DLL","GetArray"
END FUNCTION
IF 0<=s AND s<2^32 THEN
LET GetArray=getArray_sub(s)
ELSE
LET GetArray=0
END if
END FUNCTION
EXTERNAL SUB FreeArray(p)
! Returns the memory, where p is the value gotten by GetArray.
ASSIGN "BitArray.DLL","FreeArray"
END SUB
EXTERNAL FUNCTION Test(p,i)
! Gets the i-th bit, where p is the value gotten by GetArray.
ASSIGN "BitArray.DLL", "Test"
END FUNCTION
EXTERNAL SUB SetBit(p,i)
! Sets the i-th bit to 1, where p is the value gotten by GetArray.
ASSIGN "BitArray.DLL", "SetBit"
END SUB
EXTERNAL SUB ResetBit(p,i)
! Sets the i-th bit to 0, where p is the value gotten by GetArray.
ASSIGN "BitArray.DLL", "ResetBit"
END SUB
The range of the parameter that can be assigned in TestCSetBitCResetBit is from 0 to s, where s is the value that has been assigned in GetArray.
The upper limit of s that can be assigned in GetArray actually is 1983381248 when a test was performed on Windows XP.
Notice. Test, SetBit, resetBit have no measure for safety when the value beyond the range is assigned. Be careful to make a program not to assign the value beyond the range.