FreeBasic ULongInt Keyword
last modified June 16, 2025
The FreeBasic ULongInt
keyword represents an unsigned 64-bit
integer data type. It can store values from 0 to 18,446,744,073,709,551,615.
This type is useful for large positive numbers that don't need sign bits.
Basic Definition
In FreeBasic, ULongInt
is a built-in data type that occupies
8 bytes (64 bits) of memory. It can only contain non-negative integer values.
The range is from 0 to 2^64-1.
ULongInt variables are commonly used when working with large quantities, memory addresses, or when unsigned arithmetic is required. They prevent overflow issues with large positive numbers.
Declaring ULongInt Variables
This example shows how to declare and initialize ULongInt variables.
Dim population As ULongInt Dim fileSize As ULongInt = 4294967296 Dim maxValue As ULongInt = &HFFFFFFFFFFFFFFFF Print "population: "; population Print "fileSize: "; fileSize Print "maxValue: "; maxValue
Here we declare three ULongInt variables. The first is uninitialized and defaults to 0. The others are explicitly set to values. The last one uses hexadecimal notation to set the maximum possible ULongInt value.
ULongInt Arithmetic Operations
ULongInt supports standard arithmetic operations with some limitations.
Dim a As ULongInt = 18446744073709551615 Dim b As ULongInt = 10000000000 Dim result As ULongInt result = a - b Print "Subtraction: "; result result = b * 2 Print "Multiplication: "; result result = a / 2 Print "Division: "; result
This example demonstrates arithmetic with ULongInt values. Note that subtracting from the maximum value shows proper unsigned behavior. Multiplication and division work as expected within the value range.
ULongInt in Loop Counters
ULongInt can be used for very large loop counters.
Dim counter As ULongInt Dim limit As ULongInt = 10 For counter = 0 To limit Print "Counter: "; counter Next
This simple example shows a ULongInt used as a loop counter. While this example uses a small limit, ULongInt can handle extremely large loop counts. The syntax is identical to using other integer types.
ULongInt with Bitwise Operations
ULongInt supports all standard bitwise operations.
Dim flags As ULongInt = &B1010101010101010 Dim mask As ULongInt = &B1111000011110000 Print "Original: "; Bin(flags, 64) Print "AND: "; Bin(flags And mask, 64) Print "OR: "; Bin(flags Or mask, 64) Print "XOR: "; Bin(flags Xor mask, 64) Print "NOT: "; Bin(Not flags, 64)
This code demonstrates bitwise operations on ULongInt values. The Bin
function displays the binary representation. Note that NOT operation inverts
all 64 bits, showing the full range of ULongInt values.
ULongInt for File Operations
ULongInt is ideal for working with large file sizes.
Function GetFileSize(filename As String) As ULongInt Dim As ULongInt size Open filename For Binary As #1 size = LOF(1) Close #1 Return size End Function Dim size As ULongInt = GetFileSize("largefile.dat") Print "File size: "; size; " bytes" Print "File size in GB: "; size / (1024 * 1024 * 1024)
This example shows how ULongInt can handle large file sizes. The LOF
function returns a ULongInt, which we store and manipulate. The division
shows how to convert bytes to gigabytes without overflow.
ULongInt Type Conversion
FreeBasic handles conversions between ULongInt and other numeric types.
Dim bigNum As ULongInt = 5000000000 Dim regularInt As Integer = bigNum Dim doubleNum As Double = bigNum Print "ULongInt: "; bigNum Print "To Integer: "; regularInt Print "To Double: "; doubleNum
This demonstrates implicit type conversion. Converting to smaller integer types may lose precision. Converting to floating-point preserves the value but may lose some precision for very large numbers.
ULongInt Limitations
While ULongInt is versatile, it has some important limitations.
Dim max As ULongInt = &HFFFFFFFFFFFFFFFF Dim min As ULongInt = 0 Print "Max ULongInt + 1: "; max + 1 ' Wraps around to 0 Print "Min ULongInt - 1: "; min - 1 ' Wraps around to max
This example shows unsigned integer wrap-around behavior. Adding 1 to the maximum value wraps to 0. Subtracting 1 from 0 wraps to the maximum value. These behaviors are important to understand to avoid bugs.
Best Practices
- Use when needed: Only use ULongInt for very large positive numbers.
- Watch for overflow: Be aware of wrap-around behavior.
- Type suffixes: Use
ULL
suffix for ULongInt literals. - Mixed arithmetic: Be careful when mixing with signed types.
- Print formatting: Use
Print Using
for large numbers.
This tutorial covered the FreeBasic ULongInt
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.