FreeBasic Long Keyword
Last modified October 18, 2026
The FreeBasic Long keyword represents a 32-bit signed integer
data type. It can store whole numbers from -2,147,483,648 to 2,147,483,647.
Long integers are commonly used when regular integers are too small.
Basic Definition
In FreeBasic, Long is a fundamental data type that occupies
4 bytes (32 bits) of memory on all targets. It has a fixed range, independent
of the platform, and is the same size as Integer only on 32-bit
targets; on 64-bit targets Integer is 8 bytes.
The Long type is signed, meaning it can represent both positive and
negative values. It is particularly useful for calculations that might exceed the
range of smaller integer types.
Declaring Long Variables
This example shows how to declare and initialize Long variables.
Dim population As Long Dim distance As Long = 150000000 Dim accountBalance As Long = -50000 Print "population: "; population Print "distance: "; distance Print "accountBalance: "; accountBalance
Here we declare three Long variables. The first is uninitialized
and defaults to 0. The others are set to positive and negative values. A Long
holds values up to 2,147,483,647, which is more than a 16-bit Short or Byte can
hold, but on 64-bit targets it is narrower than Integer.
Long Arithmetic Operations
Long variables support all standard arithmetic operations.
Dim a As Long = 2000000000 Dim b As Long = 1000000000 Print "Addition: "; a + b Print "Subtraction: "; a - b Print "Multiplication: "; a * 2 Print "Division: "; a / 3 Print "Modulus: "; a Mod b
This example demonstrates basic arithmetic with Long values. Note that operations can exceed the Long range, potentially causing overflow. FreeBasic does not automatically prevent arithmetic overflow with Long variables.
Long in Loop Counters
Long variables can be used as loop counters for very large ranges.
Dim i As Long
For i = 2000000000 To 2000000005
Print "Counter: "; i
Next
Here we use a Long variable as a loop counter. This is useful when
the loop range exceeds the maximum value of a regular Integer. The
loop prints numbers from 2 billion to 2 billion plus 5.
Long with Arrays
Arrays can be indexed using Long variables for large collections.
Dim Shared bigArray(1000000) As Integer
Dim index As Long
For index = 0 To 1000000
bigArray(index) = index Mod 100
Next
Print "Element 500000: "; bigArray(500000)
This example shows how Long variables can index large arrays. The
array contains one million elements, and the index variable is a Long so that
it can address every element without overflow.
The Shared keyword makes the array visible across the module,
including inside procedures, which is useful for large datasets that need to be
shared across different parts of the program. It only affects scope; it does
not change where the array is allocated. A fixed-size array declared with
constant bounds is allocated with static storage, not on the stack.
Long Type Conversion
FreeBasic performs automatic conversion between Long and other numeric types.
Dim l As Long = 2147483647 Dim d As Double = l Print "Long to Double: "; d Dim newLong As Long = 3.14159 Print "Double to Long: "; newLong
This demonstrates implicit type conversion. When converting to floating-point, no precision is lost. When converting from floating-point to Long, the value is rounded to the nearest whole number (for example 3.14159 becomes 3 and -3.7 becomes -4), not truncated.
Long with Bitwise Operations
Long variables support bitwise operations for low-level
programming.
Dim flags As Long = &B11001100110011001100110011001100 Dim mask As Long = &B00000000111111110000000011111111 Print "Original: "; Bin(flags, 32) Print "AND: "; Bin(flags And mask, 32) Print "OR: "; Bin(flags Or mask, 32) Print "XOR: "; Bin(flags Xor mask, 32) Print "NOT: "; Bin(Not flags, 32)
This example shows bitwise operations on Long values. The Bin
function displays the binary representation. Bitwise operations are useful
for working with flags, masks, and binary data.
Bitwise operations allow you to manipulate individual bits of a Long value, which is essential in low-level programming, such as hardware control, network protocols, and performance optimization. They can be used to set, clear, or toggle specific bits in a Long variable. For example, you can use bitwise operations to create flags that represent different states or options in your program.
Long Function Parameters
Functions can accept and return Long values for large numeric
operations.
Function Factorial(n As Long) As Long
Dim result As Long = 1
Dim i As Long
For i = 1 To n
result = result * i
Next
Return result
End Function
Print "Factorial of 10: "; Factorial(10)
Print "Factorial of 15: "; Factorial(15)
This factorial function uses Long variables to handle larger
results. While factorials grow quickly, the Long type can store
factorials up to 12! (12! = 479001600) before overflowing. The output for
15! shown above (2004310016) is the wrapped, overflowed result. Larger values
would require LongInt (for example 15! = 1307674368000).
Best Practices
- Use when needed: Only use Long when regular Integer is insufficient.
- Watch for overflow: Be aware that operations can exceed Long range.
- Memory usage: Long is always 4 bytes. On 32-bit targets it matches Integer; on 64-bit targets Integer is 8 bytes, so Long uses half the memory of Integer.
- Type suffixes: Use & suffix for Long literals (e.g., 12345678&).
- Performance: Use Integer for general-purpose arithmetic; Long is mainly useful when a fixed 32-bit size is required.
This tutorial covered the FreeBasic Long keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.