|
Yumi Murakami
DoIt!AttachTheEarOfACat!
Join date: 27 Sep 2005
Posts: 6,860
|
02-01-2007 19:59
default { state_entry() { integer test = 2147483647; llOwnerSay("Starting with " + (string)test); test = test << 1; llOwnerSay("After shift left, " + (string)test); test = test >> 1; llOwnerSay("After shift right, " + (string)test); } }
The output is: Starting with 2147483647. After shift left, -2. After shift right, -1. Now, I can understand why shifting left would give -2 - the leftmost binary 1 is shifted into the sign bit. But surely in that case, shifting right again should shift that 1 back out of the sign bit and restore 2147483647 back? Is this a new bug?
|
|
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
|
02-01-2007 20:18
Weird but right - it's treating it as -2 divided by 2 and extending the sign bit. http://en.wikipedia.org/wiki/Arithmetic_shift
|
|
Learjeff Innis
musician & coder
Join date: 27 Nov 2006
Posts: 817
|
02-02-2007 08:48
After shift left, that's correct because 0xfffffffe is the two's complement representation for -2. Integers in LSL are signed. I don't believe there's any support for 32-bit unsigned numbers.
And after shift right, yes it's also correct assuming the right shift is a numeric shift, not a logical one -- due to sign bit propagation.
The results are what I'd expect; except that a shift right could be either signed or logical. IIRC, in C/C++, shifts on signed integers are arithmetic and do sign propagation; on unsigned integers it's logical (because there is no sign bit).
|