FreeBasic Long Keyword
last modified June 21, 2025
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. It provides a wider range than the standard
Integer type, making it suitable for larger numeric values.
The Long
type is signed, meaning it can represent both positive and
negative values. It's 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. Long
variables can store much larger numbers than regular Integer variables.
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 doesn't 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. When
working with arrays containing more than 32,767 elements, Long
indexes are necessary. The array contains one million elements.
The Shared
keyword allows the array to be accessed globally, which
is useful for large datasets that need to be shared across different parts of
the program. Also by default, FreeBasic array are stored on stack, but using the
Shared
keyword, the array is stored in the heap, allowing for
larger memory allocation.
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 truncated (decimal portion is discarded).
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. Larger values would
require ULong
.
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: Remember Long uses twice the memory of Integer.
- Type suffixes: Use & suffix for Long literals (e.g., 12345678&).
- Performance: Long operations may be slightly slower than Integer.
This tutorial covered the FreeBasic Long
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.