FreeBasic ULong Keyword
last modified June 16, 2025
The FreeBasic ULong
keyword represents an unsigned 32-bit integer
data type. It can store values from 0 to 4,294,967,295 (2^32 - 1). ULong is
useful when working with large positive numbers.
Basic Definition
In FreeBasic, ULong
is a built-in unsigned integer type that
occupies 4 bytes (32 bits) of memory. Unlike signed types, it cannot
represent negative numbers but has a larger positive range.
ULong variables are commonly used for counting, array indexing, and bit
manipulation. They prevent overflow when dealing with large positive values.
The type suffix for ULong is UL
or ul
.
Declaring ULong Variables
This example shows how to declare and initialize ULong variables.
Dim population As ULong Dim fileSize As ULong = 4294967295UL Dim counter As ULong = 1000000UL Print "population: "; population Print "fileSize: "; fileSize Print "counter: "; counter
Here we declare three ULong variables. The first is uninitialized and defaults to 0. The others are explicitly set to values. Note the UL suffix used to indicate ULong literals. This helps prevent overflow during assignment.
ULong Arithmetic Operations
ULong variables support standard arithmetic operations with some limitations.
Dim a As ULong = 4000000000UL Dim b As ULong = 300000000UL Print "Addition: "; a + b Print "Subtraction: "; a - b Print "Multiplication: "; a * 2UL Print "Division: "; a / 2UL Print "Modulus: "; a Mod b
This example demonstrates basic arithmetic with ULong values. All operations must stay within the 0 to 4,294,967,295 range. Attempting to go below zero wraps around to the maximum value due to unsigned nature.
ULong in Loops
ULong can be useful for loops with large iteration counts.
Dim i As ULong For i = 4294967290UL To 4294967295UL Print "Count: "; i Sleep 200 Next
This loop counts up to the maximum ULong value. Using ULong prevents overflow that would occur with signed types at high values. The Sleep pauses make the output readable as it counts up.
ULong with Bitwise Operations
ULong is well-suited for bit manipulation due to its fixed size.
Dim flags As ULong = &B11001100110011001100110011001100UL Print "Original: "; Bin(flags, 32) Print "AND: "; Bin(flags And &HFFFFFFFFUL, 32) Print "OR: "; Bin(flags Or &H0000000FUL, 32) Print "XOR: "; Bin(flags Xor &HAAAAAAAAUL, 32) Print "NOT: "; Bin(Not flags, 32) Print "Shift Left: "; Bin(flags Shl 4, 32) Print "Shift Right: "; Bin(flags Shr 4, 32)
This shows bitwise operations on a ULong value. The Bin function displays results in binary format. ULong's fixed 32-bit size makes it predictable for bit manipulation unlike variable-size types.
ULong Array Example
Arrays of ULong can store large quantities of unsigned data efficiently.
Dim primes(0 To 4) As ULong = {2UL, 4294967291UL, 4294967293UL, _ 4294967295UL, 7UL} For i As ULong = 0 To 4 Print "Prime "; i; ": "; primes(i) Next
This creates an array of ULong values including some very large primes near the maximum ULong value. The array indexing uses ULong for consistency, though smaller indexes would work with Integer too.
ULong Function Parameters
Functions can accept and return ULong values for large-number operations.
Function Factorial(n As ULong) As ULong If n <= 1 Then Return 1UL Return n * Factorial(n - 1UL) End Function Dim num As ULong = 12UL Print num; "! = "; Factorial(num)
This recursive factorial function uses ULong to handle larger results than signed types could. Note that factorial grows very quickly - even ULong will overflow at 13! (13 factorial exceeds ULong's maximum value).
ULong Type Conversion
FreeBasic handles conversions between ULong and other numeric types.
Dim ul As ULong = 4000000000UL Dim i As Integer = -1 Print "ULong to Integer: "; CInt(ul) Print "Integer to ULong: "; CULng(i) Print "ULong to Single: "; CSng(ul) Print "Single to ULong: "; CULng(12345.678)
This demonstrates explicit type conversion with ULong. Converting large ULong to signed types may produce unexpected results due to overflow. Negative numbers convert to large positive values when going to ULong.
Best Practices
- Use for large values: Prefer ULong when working with numbers > 2 billion.
- Unsigned operations: Be aware that subtraction can wrap around.
- Type suffixes: Use UL suffix for literals to prevent overflow.
- Loop counters: Use for very large loops where Integer might overflow.
- Bit operations: Ideal for bit manipulation due to fixed size.
This tutorial covered the FreeBasic ULong
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.