Equivalent to Java's ">>>" operator in Go?

1,197 views
Skip to first unread message

Henry Heikkinen

unread,
Feb 10, 2011, 6:53:18 PM2/10/11
to golang-nuts
I'm porting a small cryptography class from Java and it's used there
multiple times. I have no idea how to get the same effect in Go.

Rob 'Commander' Pike

unread,
Feb 10, 2011, 7:05:09 PM2/10/11
to Henry Heikkinen, golang-nuts

On Feb 10, 2011, at 3:53 PM, Henry Heikkinen wrote:

> I'm porting a small cryptography class from Java and it's used there
> multiple times. I have no idea how to get the same effect in Go.

That's unsigned shift, right? Zero fill? If you right shift an unsigned integer, Go will zero-fill. If you right shift a signed integer, it will sign-fill. If you need zero fill and you have a signed integer, just convert it to unsigned before shifting.

-rob

Henry Heikkinen

unread,
Feb 10, 2011, 7:12:12 PM2/10/11
to golang-nuts
Do you mean like this?

"c.var1 >>> 6" becomes "uint32(c.var1) >> 6"

Rob 'Commander' Pike

unread,
Feb 10, 2011, 7:23:15 PM2/10/11
to Henry Heikkinen, golang-nuts

On Feb 10, 2011, at 4:12 PM, Henry Heikkinen wrote:

> Do you mean like this?
>
> "c.var1 >>> 6" becomes "uint32(c.var1) >> 6"

Yes, but I bet it's easier just to make c.var1 a uint32 when it's declared. Java is hampered by the lack of unsigned integers. Go is not.

-rob

Henry Heikkinen

unread,
Feb 10, 2011, 7:24:53 PM2/10/11
to golang-nuts
Okay, it seems to be correct. Thank you.
Reply all
Reply to author
Forward
0 new messages