FreeBasic Integer Keyword
last modified June 21, 2025
The FreeBasic Integer
keyword represents a fundamental data type
for storing whole numbers. Integers are essential for counting, indexing,
and mathematical operations in programming.
Basic Definition
In FreeBasic, Integer
is a 32-bit signed integer data type. It
can store values from -2,147,483,648 to 2,147,483,647. Integers are stored
in two's complement format.
Integer
variables are used for calculations that don't require
decimal precision. They are faster to process than floating-point numbers and
consume less memory than larger numeric types.
Declaring Integer Variables
This example shows how to declare and initialize Integer
variables.
Dim age As Integer Dim count As Integer = 10 Dim temperature As Integer = -5 Print "age: "; age Print "count: "; count Print "temperature: "; temperature
Here we declare three Integer variables. The first is uninitialized and contains an undefined value. The others are initialized with positive and negative values. Integer variables default to 0 if declared at module level.
Integer Arithmetic Operations
Integers support standard arithmetic operations like addition and division.
Dim a As Integer = 15 Dim b As Integer = 4 Print "a + b = "; a + b Print "a - b = "; a - b Print "a * b = "; a * b Print "a / b = "; a / b Print "a Mod b = "; a Mod b
This example demonstrates basic arithmetic with Integers. Note that division of Integers produces an Integer result (truncated). The Mod operator returns the remainder of division.
Integer Overflow
Integer variables can overflow if assigned values outside their range.
Dim bigNum As Integer = 2147483647 Print "Maximum Integer: "; bigNum bigNum = bigNum + 1 Print "After overflow: "; bigNum
Adding 1 to the maximum Integer
value causes overflow, wrapping
around to the minimum value. This demonstrates why range checking is important
when working with Integers.
Integer Type Conversion
FreeBasic automatically converts between Integer
and other numeric
types.
Dim intVal As Integer = 42 Dim dblVal As Double = 3.14159 Print "Double to Integer: "; CInt(dblVal) Print "Integer to Double: "; CDbl(intVal) Print "String to Integer: "; CInt("1234")
This shows explicit conversion between types using CInt
and
CDbl
. When converting from floating-point, the value is
rounded. String conversion parses numeric strings.
Integer Arrays
Arrays of Integers are useful for storing sequences of whole numbers.
Dim numbers(1 To 5) As Integer For i As Integer = 1 To 5 numbers(i) = i * 10 Next For i As Integer = 1 To 5 Print numbers(i) Next
This creates an array of 5 Integers and fills it with multiples of 10. Arrays are zero-based by default, but we specified 1-based indexing here. Integer arrays are memory-efficient for numeric sequences.
Integer with Bitwise Operations
Integers support bit-level manipulation using bitwise operators.
Dim a As Integer = &b1100 ' 12 in decimal Dim b As Integer = &b1010 ' 10 in decimal Print "a AND b: "; a And b Print "a OR b: "; a Or b Print "NOT a: "; Not a Print "a XOR b: "; a Xor b Print "a shifted left: "; a Shl 1 Print "b shifted right: "; b Shr 1
This demonstrates bitwise operations on Integers. The results are shown in decimal. Bitwise operations are useful for low-level programming and optimization tasks.
Integer in Function Parameters
Functions can accept and return Integer values.
Function Square(n As Integer) As Integer Return n * n End Function Function Sum(a As Integer, b As Integer) As Integer Return a + b End Function Print "Square of 5: "; Square(5) Print "Sum of 3 and 7: "; Sum(3, 7)
These functions demonstrate passing and returning Integer values. Integer parameters are passed by value by default. The functions perform basic mathematical operations and return Integer results.
Best Practices
- Range Checking: Validate values to prevent overflow.
- Initialization: Always initialize Integer variables.
- Type Selection: Use appropriate-sized integers for needs.
- Constants: Use named constants for magic numbers.
- Comments: Document assumptions about integer ranges.
This tutorial covered the FreeBasic Integer
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.