res*_*a87 3 binary-operators go
Go 中是否有执行无符号移位(即无符号右移)操作?Java 中是这样的
0xFF >>> 3
Run Code Online (Sandbox Code Playgroud)
关于此事我唯一能找到的就是这篇文章,但我不确定我必须做什么。
提前致谢。
数字类型表示整数或浮点值的集合。预先声明的独立于体系结构的数字类型包括:
Run Code Online (Sandbox Code Playgroud)uint8 the set of all unsigned 8-bit integers (0 to 255) uint16 the set of all unsigned 16-bit integers (0 to 65535) uint32 the set of all unsigned 32-bit integers (0 to 4294967295) uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615) int8 the set of all signed 8-bit integers (-128 to 127) int16 the set of all signed 16-bit integers (-32768 to 32767) int32 the set of all signed 32-bit integers (-2147483648 to 2147483647) int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807) byte alias for uint8 rune alias for int32n 位整数的值是 n 位宽,并使用二进制补码算术表示。
还有一组具有特定于实现的大小的预先声明的数字类型:
Run Code Online (Sandbox Code Playgroud)uint either 32 or 64 bits int same size as uint uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value当不同的数值类型混合在表达式或赋值中时,需要进行转换。
Run Code Online (Sandbox Code Playgroud)<< left shift integer << unsigned integer >> right shift integer >> unsigned integer移位运算符将左操作数移位右操作数指定的移位计数。如果左操作数是有符号整数,则它们实现算术移位;如果左操作数是无符号整数,则它们实现逻辑移位。轮班数没有上限。移位的行为就像将左操作数移位 n 次,移位次数为 n。因此,x << 1 与 x*2 相同,x >> 1 与 x/2 相同,但朝负无穷大截断。
在 Go 中,它是无符号整数移位。Go 有符号和无符号整数。
这取决于该值0xFF是什么类型。假设它是无符号整数类型之一,例如uint.
package main
import "fmt"
func main() {
n := uint(0xFF)
fmt.Printf("%X\n", n)
n = n >> 3
fmt.Printf("%X\n", n)
}
Run Code Online (Sandbox Code Playgroud)
输出:
FF
1F
Run Code Online (Sandbox Code Playgroud)
假设它是有符号整数类型之一,例如int.
package main
import "fmt"
func main() {
n := int(0xFF)
fmt.Printf("%X\n", n)
n = int(uint(n) >> 3)
fmt.Printf("%X\n", n)
}
Run Code Online (Sandbox Code Playgroud)
输出:
FF
1F
Run Code Online (Sandbox Code Playgroud)