dongleiwei2182 2015-10-25 23:33
浏览 28

开始-执行无符号移位操作

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条回答 默认 最新

  • duanlei20082008 2015-10-26 00:14
    关注

    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 "fmt"
    
    func main() {
        n := uint(0xFF)
        fmt.Printf("%X
    ", n)
        n = n >> 3
        fmt.Printf("%X
    ", n)
    }
    

    Output:

    FF
    1F
    

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

    package main
    
    import "fmt"
    
    func main() {
        n := int(0xFF)
        fmt.Printf("%X
    ", n)
        n = int(uint(n) >> 3)
        fmt.Printf("%X
    ", n)
    }
    

    Output:

    FF
    1F
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?