FreeBasic UShort Keyword
last modified June 16, 2025
The FreeBasic UShort
keyword represents an unsigned 16-bit
integer data type. It can store values from 0 to 65,535. UShort is useful
for memory-efficient storage of positive numbers.
Basic Definition
In FreeBasic, UShort
is a built-in data type that occupies
2 bytes (16 bits) of memory. It can only contain non-negative integer
values in the range 0 to 65,535.
UShort variables are commonly used when working with binary data, file formats, or when memory optimization is important. They prevent negative values while maintaining a compact size.
Declaring UShort Variables
This example shows how to declare and initialize UShort variables.
Dim age As UShort Dim population As UShort = 42000 Dim maxValue As UShort = 65535 Print "age: "; age Print "population: "; population Print "maxValue: "; maxValue
Here we declare three UShort variables. The first is uninitialized and defaults to 0. The others are explicitly set to values within the valid range. Attempting to assign a negative value would cause an overflow.
UShort Arithmetic Operations
UShort variables can participate in standard arithmetic operations.
Dim a As UShort = 40000 Dim b As UShort = 25500 Dim result As UShort result = a + b Print "Addition: "; result result = a - b Print "Subtraction: "; result result = a * 2 Print "Multiplication: "; result result = a \ 10 Print "Integer division: "; result
This example demonstrates basic arithmetic with UShort values. Note that adding 40000 and 25500 exceeds the UShort range, causing overflow. FreeBasic will wrap around the value in this case.
UShort in Loop Counters
UShort can be used as loop counters for small iteration ranges.
Dim i As UShort For i = 1 To 10 Print "Iteration: "; i Next Dim count As UShort = 0 While count < 5 Print "While loop count: "; count count += 1 Wend
Here we use UShort variables in both For and While loops. This is memory efficient when you know the loop count won't exceed 65,535 iterations. For larger loops, consider using Integer or UInteger instead.
UShort with Arrays
Arrays of UShort values can be useful for memory-efficient storage.
Dim temperatures(7) As UShort temperatures(0) = 250 ' 25.0°C scaled by 10 temperatures(1) = 255 temperatures(2) = 260 For i As UShort = 0 To 2 Print "Temperature "; i; ": "; temperatures(i)/10; "°C" Next
This example creates an array to store temperature readings. We use UShort to store scaled values (25.0°C as 250) for precision while saving memory. Each element takes only 2 bytes instead of 4 bytes for Integer.
UShort with Binary Operations
UShort is often used in bitwise operations due to its fixed 16-bit size.
Dim flags As UShort = &b1010101010101010 Dim mask As UShort = &b0000000011111111 Print "Original: "; Bin(flags, 16) Print "AND with mask: "; Bin(flags And mask, 16) Print "OR with mask: "; Bin(flags Or mask, 16) Print "XOR with mask: "; Bin(flags Xor mask, 16) Print "NOT: "; Bin(Not flags, 16)
This demonstrates bitwise operations on UShort values. The binary format shows the exact bit patterns. UShort is ideal for such operations because its size is guaranteed to be 16 bits across platforms.
UShort Type Conversion
FreeBasic can convert between UShort and other numeric types.
Dim smallNum As UShort = 32000 Dim largeNum As Integer = 40000 ' Implicit conversion from UShort to Integer Dim result1 As Integer = smallNum ' Explicit conversion from Integer to UShort Dim result2 As UShort = CUShort(largeNum) Print "UShort to Integer: "; result1 Print "Integer to UShort: "; result2
This shows type conversion involving UShort. Implicit conversion to larger
types is safe, but converting from larger types requires caution. The
CUShort
function ensures explicit conversion when needed.
UShort with File I/O
UShort is commonly used when reading binary files with 16-bit values.
Dim fileNum As Integer = FreeFile() Dim values(2) As UShort = {1000, 2000, 3000} ' Write UShort values to binary file Open "data.bin" For Binary As #fileNum Put #fileNum, , values Close #fileNum ' Read UShort values back Dim readValues(2) As UShort Open "data.bin" For Binary As #fileNum Get #fileNum, , readValues Close #fileNum For i As Integer = 0 To 2 Print "Value "; i; ": "; readValues(i) Next
This example writes and reads an array of UShort values to a binary file. UShort is ideal for binary file operations as it matches common 16-bit formats. The values are stored exactly as their binary representation.
Best Practices
- Range Checking: Always validate values fit in 0-65535 range.
- Memory Efficiency: Use UShort for large arrays of small numbers.
- Binary Data: Prefer UShort when working with 16-bit binary formats.
- Type Conversion: Use CUShort for explicit conversions.
- Documentation: Comment when UShort is used for specific formats.
This tutorial covered the FreeBasic UShort
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.