Order of Operations (OO)

Perhaps not due to OO but another matter. I was attempting to implement Pat Morin’s Radix Sort implementation when I ran into an issue involving the first for loops expression:

c[(a[i] >> d*p)&((1<<d)-1)]++;

It seems In Go the first half of the expression (a[i] >> d*p) evaluates like so ((a[i] >> d) * p) as opposed to languages like Java, Python, & C that evaluate it like so: (a[i] >> (d * p)). I haven’t tested how the second half of the expression evaluates (it’s quite late) But I imagine it’s the same. Swift seems to follow the same order logic as Go.

My question is , is this intentional ? I thought that by Go’s order of operations that multiplication would execute first. I feel like there’s something I’m missing or maybe it’s just how Go does things.

Link to code

<</>> have the same precedence as *:

Precedence    Operator
    5             *  /  %  <<  >>  &  &^
    4             +  -  |  ^
    3             ==  !=  <  <=  >  >=
    2             &&
    1             ||

The Go Programming Language Specification - The Go Programming Language (hope this link works correctly)

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.