Golang uint16 类型
最后修改时间 2025 年 5 月 8 日
本教程将解释如何在 Go 中使用 `uint16` 内置类型。我们将通过实际示例介绍 16 位无符号整数的基本概念。
`uint16` 类型代表 Go 中的 16 位无符号整数。它可以存储从 0 到 65535 的值。当内存效率很重要且值范围已知时,此类型很有用。
在 Go 中,`uint16` 是无符号整数类型系列的一部分。它通常用于二进制协议、网络编程和嵌入式系统,在这些场景中,精确的大小很重要。
基本的 uint16 声明和用法
`uint16` 最简单的用法包括变量声明和基本算术运算。此示例展示了基本的 uint16 用法。
package main import "fmt" func main() { var a uint16 = 1000 var b uint16 = 2000 sum := a + b fmt.Printf("a: %d, b: %d, sum: %d\n", a, b, sum) fmt.Printf("Type of sum: %T\n", sum) // Division example quotient := b / a fmt.Printf("2000 / 1000 = %d\n", quotient) }
该示例演示了 uint16 值之间的基本算术运算。请注意,除非显式转换,否则所有运算都保持在 uint16 类型内。
处理 uint16 溢出
由于 uint16 具有固定的范围,因此可能会发生溢出。此示例显示了 Go 如何处理 uint16 溢出情况。
package main import "fmt" func main() { max := uint16(65535) fmt.Println("Max uint16:", max) // This would overflow // next := max + 1 // Compile-time error if uncommented // Runtime overflow wraps around next := max next++ fmt.Println("After overflow:", next) // Underflow example zero := uint16(0) zero-- fmt.Println("After underflow:", zero) }
Go 通过回绕来处理 uint16 溢出。值 65535 + 1 变为 0,而 0 - 1 变为 65535。这是无符号整数的标准行为。
在二进制操作中使用 uint16
uint16 类型对于位运算特别有用。此示例演示了 uint16 的常见位操作模式。
package main import "fmt" func main() { flags := uint16(0b1010101010101010) fmt.Printf("Initial flags: %016b\n", flags) // Set bit 3 flags |= 1 << 3 fmt.Printf("After setting bit 3: %016b\n", flags) // Clear bit 5 flags &^= 1 << 5 fmt.Printf("After clearing bit 5: %016b\n", flags) // Toggle bit 7 flags ^= 1 << 7 fmt.Printf("After toggling bit 7: %016b\n", flags) // Check bit 10 if flags&(1<<10) != 0 { fmt.Println("Bit 10 is set") } else { fmt.Println("Bit 10 is not set") } }
与 uint16 相比,位运算效率很高。该示例展示了在 uint16 值中设置、清除、切换和检查单个位。
uint16 在网络编程中的应用
uint16 类型在网络编程中常用于端口号和协议字段。此示例在网络上下文中演示了 uint16。
package main import ( "encoding/binary" "fmt" "net" ) func main() { // Representing a port number port := uint16(8080) fmt.Printf("Port number: %d\n", port) // Converting to network byte order (big-endian) buf := make([]byte, 2) binary.BigEndian.PutUint16(buf, port) fmt.Printf("Network byte order: % x\n", buf) // Parsing back from network byte order restoredPort := binary.BigEndian.Uint16(buf) fmt.Printf("Restored port: %d\n", restoredPort) // Common network constants fmt.Printf("HTTP port: %d, HTTPS port: %d\n", 80, 443) }
网络协议通常使用 uint16 来表示端口号和类似字段。`binary` 包有助于进行字节序转换。
uint16 在数组和切片中的应用
处理 uint16 值集合可以展示内存效率。此示例显示了 uint16 数组和切片。
package main import ( "fmt" "unsafe" ) func main() { // Array of uint16 values data := [5]uint16{10, 20, 30, 40, 50} fmt.Println("Array:", data) // Slice of uint16 values slice := data[1:4] fmt.Println("Slice:", slice) // Memory usage comparison var ui16 uint16 var ui32 uint32 fmt.Printf("Size of uint16: %d bytes\n", unsafe.Sizeof(ui16)) fmt.Printf("Size of uint32: %d bytes\n", unsafe.Sizeof(ui32)) // Processing slice elements sum := uint16(0) for _, v := range slice { sum += v } fmt.Println("Sum of slice elements:", sum) }
与较大的整数类型相比,在数组和切片中使用 uint16 可以节省内存。该示例显示了声明、切片和迭代。
来源
本教程通过在各种编程上下文中的实际用例,涵盖了 Go 中的 `uint16` 类型。