duanrong5167
2017-11-30 07:35
浏览 121
已采纳

Golang位只使用无符号?

This is a java function, i converted to golang code, but appear error.
How can I solve this problem? please teach.

java code:

int p = -1;
int x = 0;
x |= 0x1 << p;
p++;

golang code:

var p int = -1
var x int = 0
x |= 0x1 << p
p++

shift code type int, must be unsigned integer

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqian8265 2017-11-30 07:46
    已采纳

    This is an error because as per Go specifications, bit shifting only works on unsigned ints:

    left shift             integer << unsigned integer
    right shift            integer >> unsigned integer
    
    点赞 打赏 评论

相关推荐