ZetCode

FreeBasic UShort 关键字

最后修改日期:2025 年 6 月 16 日

FreeBasic 的 UShort 关键字表示一个无符号的 16 位整数数据类型。它可以存储从 0 到 65,535 的值。UShort 对于内存高效地存储正数非常有用。

基本定义

在 FreeBasic 中,UShort 是一个内置的数据类型,占用 2 字节(16 位)内存。它只能包含 0 到 65,535 范围内的非负整数值。

UShort 变量通常在处理二进制数据、文件格式或内存优化很重要时使用。它们可以防止负值,同时保持紧凑的大小。

声明 UShort 变量

此示例演示如何声明和初始化 UShort 变量。

ushort_declare.bas
Dim age As UShort
Dim population As UShort = 42000
Dim maxValue As UShort = 65535

Print "age: "; age
Print "population: "; population
Print "maxValue: "; maxValue

这里我们声明了三个 UShort 变量。第一个未初始化,默认为 0。其他两个被显式设置为有效范围内的值。尝试赋值负值会导致溢出。

UShort 算术运算

UShort 变量可以参与标准的算术运算。

ushort_arithmetic.bas
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

此示例演示了 UShort 值的基本算术运算。请注意,将 40000 和 25500 相加会超出 UShort 的范围,导致溢出。在这种情况下,FreeBasic 会进行循环(wrap around)。

循环计数器中的 UShort

UShort 可用作小迭代范围的循环计数器。

ushort_loop.bas
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

这里我们在 For 和 While 循环中使用 UShort 变量。当您知道循环次数不会超过 65,535 次迭代时,这在内存方面非常高效。对于较大的循环,请考虑使用 Integer 或 UInteger。

数组中的 UShort

UShort 值数组在内存高效存储方面很有用。

ushort_array.bas
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

此示例创建了一个数组来存储温度读数。我们使用 UShort 来存储缩放值(25.0°C 表示为 250)以获得精度,同时节省内存。每个元素仅占用 2 字节,而不是 Integer 的 4 字节。

二进制运算中的 UShort

由于其固定的 16 位大小,UShort 通常用于位运算。

ushort_binary.bas
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)

这演示了 UShort 值的位运算。二进制格式显示了精确的位模式。UShort 非常适合此类操作,因为它的 16 位大小在不同平台之间是保证不变的。

UShort 类型转换

FreeBasic 可以在 UShort 和其他数字类型之间进行转换。

ushort_conversion.bas
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

这显示了涉及 UShort 的类型转换。隐式转换为较大的类型是安全的,但从较大的类型转换需要谨慎。CUShort 函数确保在需要时进行显式转换。

文件 I/O 中的 UShort

在读取具有 16 位值的二进制文件时,UShort 通常被使用。

ushort_file.bas
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

此示例将 UShort 值数组写入二进制文件并从中读取。UShort 非常适合二进制文件操作,因为它符合常见的 16 位格式。值以其二进制表示形式精确存储。

最佳实践

本教程通过实际示例涵盖了 FreeBasic 的 UShort 关键字,展示了其在不同场景下的用法。

作者

我叫 Jan Bodnar,是一位充满热情的程序员,拥有丰富的编程经验。我从 2007 年开始撰写编程文章。迄今为止,我已撰写了 1400 多篇文章和 8 本电子书。我在教授编程方面拥有十多年的经验。

列出所有 FreeBasic 教程