ZetCode

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.

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

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

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

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

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

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

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

This tutorial covered the FreeBasic ULongInt keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.