ZetCode

FreeBasic LongInt Keyword

last modified June 21, 2025

The FreeBasic LongInt keyword represents a 64-bit signed integer data type. It can store much larger numbers than standard Integer types. LongInt is essential for calculations requiring large number ranges.

Basic Definition

In FreeBasic, LongInt is a 64-bit signed integer data type. It occupies 8 bytes of memory and can store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

LongInt variables are used when standard 32-bit integers are insufficient. They provide greater range for mathematical operations and large counters.

Declaring LongInt Variables

This example shows how to declare and initialize LongInt variables.

longint_declare.bas
Dim population As LongInt
Dim distance As LongInt = 149597870700  ' Earth-Sun distance in meters
Dim bigNumber As LongInt = 9223372036854775807

Print "population: "; population
Print "distance: "; distance
Print "bigNumber: "; bigNumber

Here we declare three LongInt variables. The first is uninitialized and defaults to 0. The others are set to large values that exceed standard Integer range. LongInt can handle these large numbers without overflow.

LongInt Arithmetic Operations

LongInt supports all standard arithmetic operations with large numbers.

longint_arithmetic.bas
Dim a As LongInt = 5000000000
Dim b As LongInt = 3000000000

Print "Addition: "; a + b
Print "Subtraction: "; a - b
Print "Multiplication: "; a * b
Print "Division: "; a / b
Print "Modulus: "; a Mod b

This example demonstrates arithmetic operations with LongInt values. The numbers used are too large for standard Integer types. LongInt handles these operations correctly without overflow errors.

LongInt in Loops

LongInt can be used as loop counters for very large iterations.

longint_loop.bas
Dim i As LongInt
Dim limit As LongInt = 5000000000

For i = 1 To limit Step 1000000000
    Print "Iteration: "; i
Next

Here we use a LongInt variable as a loop counter. The loop runs up to 5 billion iterations, which would overflow a standard Integer. The Step value is also large to demonstrate LongInt's capacity.

LongInt with Functions

Functions can accept and return LongInt values for large number processing.

longint_function.bas
Function Factorial(n As LongInt) As LongInt
    Dim result As LongInt = 1
    Dim i As LongInt
    
    For i = 1 To n
        result = result * i
    Next
    
    Return result
End Function

Dim num As LongInt = 20
Print num; "! = "; Factorial(num)

The Factorial function calculates factorials using LongInt. Factorials grow very quickly and exceed standard Integer limits. LongInt can handle larger factorial values before overflowing.

LongInt Array

Arrays of LongInt can store large datasets of big numbers.

longint_array.bas
Dim bigNumbers(5) As LongInt
bigNumbers(0) = 10000000000
bigNumbers(1) = 20000000000
bigNumbers(2) = 30000000000
bigNumbers(3) = 40000000000
bigNumbers(4) = 50000000000

For i As Integer = 0 To 4
    Print "Element "; i; ": "; bigNumbers(i)
Next

This example creates an array of LongInt values. Each element stores a number too large for standard Integer types. The array can be processed like any other numeric array in FreeBasic.

LongInt Type Conversion

LongInt can be converted to other numeric types with appropriate casting.

longint_conversion.bas
Dim bigVal As LongInt = 2147483648  ' Exceeds Integer range
Dim intVal As Integer = CInt(bigVal)
Dim dblVal As Double = CDbl(bigVal)

Print "Original LongInt: "; bigVal
Print "Converted to Integer: "; intVal
Print "Converted to Double: "; dblVal

This demonstrates converting LongInt to smaller types. Converting to Integer may cause overflow if the value is too large. Double can store the full precision but loses integer accuracy for very large values.

LongInt with Bitwise Operations

LongInt supports bitwise operations on 64-bit values.

longint_bitwise.bas
Dim mask As LongInt = &HFFFF0000FFFF0000
Dim value As LongInt = &H123456789ABCDEF0

Print "Original: "; Hex(value)
Print "AND: "; Hex(value And mask)
Print "OR: "; Hex(value Or mask)
Print "XOR: "; Hex(value Xor mask)
Print "NOT: "; Hex(Not value)

This example shows bitwise operations on LongInt values. The hexadecimal constants demonstrate 64-bit patterns. Bitwise operations work on all 64 bits of the LongInt value.

Best Practices

This tutorial covered the FreeBasic LongInt keyword with practical examples showing its usage in different scenarios requiring large integers.

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.