July 27, 2004 Re: >>> | ||||
---|---|---|---|---|
| ||||
Posted in reply to Berin Loritsch | Berin Loritsch schrieb:
> Ok, Signed SHift == Roll Over, and Unsigned Shift == shift
No. it's *not* a roll!
Read again, or consult someone who can explain it better. The *leftmost* bit gets replicated, not the one which is shifted out!
-eye
|
July 28, 2004 Re: >>> | ||||
---|---|---|---|---|
| ||||
Posted in reply to Berin Loritsch | On Tue, 27 Jul 2004 09:37:56 -0400, Berin Loritsch wrote: > Arcane Jill wrote: > >> The docs say: >> "<< is a left shift. >> is a signed right shift. >>> is an unsigned right >> shift." >> > > Question: what is the difference between a signed shift and an unsigned shift? > > I know there is a difference between a Roll-Over and a Shift. > > A simple shift will set the overflow flag with the bit shoved off the end and simply fill in zeros in the empty space. For example: > > 00001111 >> 1 == 00000111 > > A roll over will operate the same way the shift does, except it uses the value of the overflow flag to fill in the empty space. This results in what appears to be a roll over: > > 00001111 >>> 1 == 10000111 > > These are two separate operations, with very different results. This is why Java really has the >> and >>> operators. It has nothing to do with signed and unsigned numbers. It has to do with predictable roll over vs. shift operations. > > These are bitwise operations and really have nothing to do with sign. > > Most folks know that the shift function is a Q&D multiply/divide by 2. The roll over is different, and used in hash functions among other things. I thought the difference was that in unsigned-shift-left, all bits were shifted but in signed-shift-left, all *except* the left-most bit were shifted. 10000011 >> 10000001 10000011 >>> 01000001 The net result is the signed shift keeps the sign of the operand and an unsigned shift will always result in a positive value. -- Derek Melbourne, Australia 28/Jul/04 9:58:36 AM |
July 28, 2004 Re: >>> | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | In article <ce6qk5$2ohe$1@digitaldaemon.com>, Derek Parnell says... > >I thought the difference was that in unsigned-shift-left, all bits were shifted but in signed-shift-left, all *except* the left-most bit were shifted. > > 10000011 >> 10000001 > 10000011 >>> 01000001 > >The net result is the signed shift keeps the sign of the operand and an unsigned shift will always result in a positive value. In fact, it goes like this: # 10000010 >> 1 = 11000001 (if the LHS is signed) # 10000010 >> 1 = 01000001 (if the LHS is unsigned) # 10000010 >>> 1 = 01000001 (regardless of the sign of the LHS) It's actually a documentation error, the claim that >> is a "signed" shift right. In fact, it appears to be the same as C's >> (which is good). Arcane Jill |
July 28, 2004 Re: >>> | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | On Wed, 28 Jul 2004 07:24:25 +0000 (UTC), Arcane Jill wrote: > In article <ce6qk5$2ohe$1@digitaldaemon.com>, Derek Parnell says... >> >>I thought the difference was that in unsigned-shift-left, all bits were shifted but in signed-shift-left, all *except* the left-most bit were shifted. >> >> 10000011 >> 10000001 >> 10000011 >>> 01000001 >> >>The net result is the signed shift keeps the sign of the operand and an unsigned shift will always result in a positive value. > > In fact, it goes like this: > > # 10000010 >> 1 = 11000001 (if the LHS is signed) > # 10000010 >> 1 = 01000001 (if the LHS is unsigned) > # 10000010 >>> 1 = 01000001 (regardless of the sign of the LHS) > > It's actually a documentation error, the claim that >> is a "signed" shift right. In fact, it appears to be the same as C's >> (which is good). Oh, okay. But I don't think I'd be wanting to bit shifts on 'signed' expressions anyway. -- Derek Melbourne, Australia 28/Jul/04 5:31:53 PM |
Copyright © 1999-2021 by the D Language Foundation