Go – 执行无符号位移操作

huangapple go评论175阅读模式
英文:

Go - Perform unsigned shift operation

问题

在Go语言中,没有直接支持无符号位移(即无符号右移)操作的运算符。然而,你可以通过使用位操作来模拟无符号右移操作。

在Go语言中,右移操作符>>是有符号右移操作符,它会保留符号位。如果你想要执行无符号右移操作,可以先将数值转换为无符号类型,然后再进行右移操作。

下面是一个示例代码:

package main

import "fmt"

func main() {
    var num uint8 = 0xFF
    shift := uint8(3)
    result := num >> shift
    fmt.Printf("%08b\n", result)
}

在这个示例中,我们将num声明为uint8类型,表示一个无符号8位整数。然后,我们将shift声明为uint8类型,表示右移的位数。通过将num右移shift位,我们可以得到无符号右移的结果。最后,我们使用fmt.Printf函数将结果以二进制形式打印出来。

希望对你有帮助!

英文:

Is there anyway to perform an unsigned shift (namely, unsigned right shift) operation in Go? Something like this in Java

0xFF >>> 3

The only thing I could find on this matter is this post but I'm not sure what I have to do.

Thanks in advance.

答案1

得分: 5

《Go编程语言规范》

数值类型

数值类型表示整数或浮点数值的集合。
预声明的与体系结构无关的数值类型包括:

uint8       所有无符号 8 位整数的集合(0 到 255)
uint16      所有无符号 16 位整数的集合(0 到 65535)
uint32      所有无符号 32 位整数的集合(0 到 4294967295)
uint64      所有无符号 64 位整数的集合(0 到 18446744073709551615)

int8        所有有符号 8 位整数的集合(-128 到 127)
int16       所有有符号 16 位整数的集合(-32768 到 32767)
int32       所有有符号 32 位整数的集合(-2147483648 到 2147483647)
int64       所有有符号 64 位整数的集合(-9223372036854775808 到 9223372036854775807)

byte        uint8 的别名
rune        int32 的别名

n 位整数的值是 n 位宽,并使用二进制补码算术表示。

还有一组具有实现特定大小的预声明数值类型:

uint     32 位或 64 位
int      与 uint 大小相同
uintptr  无符号整数,足够大以存储指针值的未解释位

在表达式或赋值中混合使用不同的数值类型时,需要进行转换。

算术运算符

<<   左移位             整数 << 无符号整数
>>   右移位             整数 >> 无符号整数

移位运算符将左操作数按右操作数指定的位数进行移位。如果左操作数是有符号整数,则实现算术移位;如果左操作数是无符号整数,则实现逻辑移位。移位计数没有上限。移位的行为相当于将左操作数按 1 进行 n 次移位。因此,x << 1 等同于 x*2,x >> 1 等同于 x/2,但向负无穷截断。

英文:

> The Go Programming Language Specification
>
> Numeric types
>
> A numeric type represents sets of integer or floating-point values.
> The predeclared architecture-independent numeric types include:
>
> 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 int32
>
> The value of an n-bit integer is n bits wide and represented using
> two's complement arithmetic.
>
> There is also a set of predeclared numeric types with
> implementation-specific sizes:
>
> 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
>
> Conversions are required when different numeric types are mixed in an
> expression or assignment.
>
> Arithmetic operators
>
> << left shift integer << unsigned integer
> >> right shift integer >> unsigned integer
>
> The shift operators shift the left operand by the shift count
> specified by the right operand. They implement arithmetic shifts if
> the left operand is a signed integer and logical shifts if it is an
> unsigned integer. There is no upper limit on the shift count. Shifts
> behave as if the left operand is shifted n times by 1 for a shift
> count of n. As a result, x << 1 is the same as x*2 and x >> 1 is the
> same as x/2 but truncated towards negative infinity.

In Go, it's an unsigned integer shift. Go has signed and unsigned integers.

It depends on what type the value 0xFF is. Assume it's one of the unsigned integer types, for example, uint.

package main

import &quot;fmt&quot;

func main() {
	n := uint(0xFF)
	fmt.Printf(&quot;%X\n&quot;, n)
	n = n &gt;&gt; 3
	fmt.Printf(&quot;%X\n&quot;, n)
}

Output:

FF
1F

Assume it's one of the signed integer types, for example, int.

package main

import &quot;fmt&quot;

func main() {
	n := int(0xFF)
	fmt.Printf(&quot;%X\n&quot;, n)
	n = int(uint(n) &gt;&gt; 3)
	fmt.Printf(&quot;%X\n&quot;, n)
}

Output:

FF
1F

huangapple
  • 本文由 发表于 2015年10月26日 07:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/33336336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定