> Date: Mon, 30 Dec 2013 18:22:37 +0000
> From: [email protected]
> To: [email protected]
> Subject: [ast-users] Bad math for right shift (>>) > 64 bits?
> 
> Not clear on whether ksh93's right shift (>>) is arithmetic or logical, as 
> that may affect the outcome here, but here's what I'm observing:
> 
> This is for ksh93 version:
> # print ${.sh.version}
> Version JM 93u 2011-02-08
> 
> Noticing that when we right shift a value, things seem to work right 
> initially:
> 
> p110 # print $(( 2**32 >> 31))
> 2
> p110 # print $(( 2**32 >> 32))
> 1
> p110 # print $(( 2**32 >> 33))
> 0
> ...
> p110 # print $(( 2**32 >> 62 ))
> 0
> p110 # print $(( 2**32 >> 63 ))
> 0
> p110 # print $(( 2**32 >> 64 ))
> 4294967296
> 
> Eh?  What's that?
> 
> And this continues on again for a while until we hit 97:
> 
> p110 # print $(( 2**32 >> 93 ))
> 8
> p110 # print $(( 2**32 >> 94 ))
> 4
> p110 # print $(( 2**32 >> 95 ))
> 2
> p110 # print $(( 2**32 >> 96 ))
> 1
> p110 # print $(( 2**32 >> 97 ))
> 0
> p110 # print $(( 2**32 >> 98 ))
> 0
> 
> Is this signed, or non-integer math, causing this behavior?
> 
> To my mind, once you right shift away all your set bits, you should get 0 
> forever, regardless of how much further you right shift.
> 
> Is this a bug, expected behavior, or what?

Looks like a bug to me.

Interested in the behaviour I tried that with my version (Version JM 93t+ 
2010-03-05( and it gives the same result.
Moreover I noticed that in

integer    b=$((2**32)) ; for ((i=0;i<=97;i++)) ; do printf "%5d\t%12d\n" $i 
$(( b >> i )) ; done
typeset -i b=$((2**32)) ; for ((i=0;i<=97;i++)) ; do printf "%5d\t%12d\n" $i 
$(( b >> i )) ; done

the first command produce the same result that you showed, while the second one 
produces only zeroes.
Moreover, if I run the second command first (and get the zeroes), then run the 
first command I get also zeroes, and if I rund the second command a second time 
it produces the result you observed. So there even seems to be a dependency to 
some internal shell state.

(I haven't checked the behaviour of a more recent ksh version in that respect.)

Janis

> 
> Thanks!
> 
> Gordon Marler
> [email protected]
> _______________________________________________
> ast-users mailing list
> [email protected]
> http://lists.research.att.com/mailman/listinfo/ast-users
                                          
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to