lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Enrico Colombini wrote:
On 25/03/2014 21.05, Tim Hill wrote:
That said, for this project I cannot recall needing arithmetic shift,
just logical.

Probably because logical shifts are bitwise operations acting on bit arrays of
defined length, assuming no intepretation of their meaning and therefore having
many different applications where they cannot easily be replaced, while
arithmetic shifts are just a shortcut for arithmetic operations on integer
values coded in a specific way: two different beasts.

I don't use arithmetical shifts in C because thy are not guaranteed and because
their meaning is, in fact, an integer division of a signed value by an unsigned
power of two. I find it safer and clearer to write the latter.

Of course there may be particolr cases where one may want to operate on signed
integer values at bit level, but they are much less common than generic bit
manipulation or bit level operations on unsigned values.

I agree with Enrico here. In my own code, if I happen to be dividing by a power of 2, I still use the actual division operator and count on the compiler to do the right thing if it is smart enough to recognize what I'm doing.

I have spent too may hours reverse engineering code that used shifts for division when the algorithm changes to use something other than a power of two.

So count me as sitting on the fence on this one, but we'll be seeing many experienced programmers scratching their heads when -1 >> 1 does the unexpected. First time programmers are less likely to have the problem :-)

Ralph