digitalmars.D - >>>
- Arcane Jill (22/22) Jul 27 2004 The docs say:
- Berin Loritsch (19/23) Jul 27 2004 Question: what is the difference between a signed shift and an unsigned
- Berin Loritsch (4/26) Jul 27 2004 Oops, I forgot to mention that most processors also have a BOF (Branch
- Ilya Minkov (11/13) Jul 27 2004 To fill up the "empty" bit which appears when shifting right, unsigned
- Berin Loritsch (2/19) Jul 27 2004 Ok, Signed SHift == Roll Over, and Unsigned Shift == shift
- Ilya Minkov (5/6) Jul 27 2004 No. it's *not* a roll!
- Arcane Jill (13/34) Jul 27 2004 I would have expected you to know this, coming from a Java background. J...
- Berin Loritsch (6/44) Jul 27 2004 Honestly, I have always parsed this in light of my old assembly
- Derek Parnell (12/45) Jul 27 2004 I thought the difference was that in unsigned-shift-left, all bits were
- Arcane Jill (8/15) Jul 28 2004 In fact, it goes like this:
- Derek Parnell (7/27) Jul 28 2004 Oh, okay. But I don't think I'd be wanting to bit shifts on 'signed'
- Jan-Eric Duden (4/39) Jul 27 2004 Maybe the operator was introduced because Java has it??
- Kazuhiro Inaba (4/5) Jul 27 2004 http://www.digitalmars.com/d/ctod.html#ushr ?
- Arcane Jill (10/15) Jul 27 2004 Aha - I missed that. Thanks for pointing that out. It does make perfect ...
The docs say: "<< is a left shift. >> is a signed right shift. >>> is an unsigned right shift." Now, here's the thing: We don't have separate operators for signed multiply and unsigned multiply. Why not? Because one operator does both jobs, based on the type of its arguments. We don't have separate operators for signed divide and unsigned divide. Why not? Because one operator does both jobs, based on the type of its arguments. Same goes for %. And yet, we DO have separater operators for signed right shift and unsigned right shift. C doesn't. C++ doesn't. But D does. ? And, if I have understood the documentation correctly, these operators the actual sign of the arguments (which means that D's (uint >> uint) will behave differently from C). I believe that Java has both >> and >>> - but with good reason. Java has no unsigned integers, and so you need a separate operator (>>>) to do unsigned shift right. This logic does not apply to D. Is there a point to all this? Or is it just a case of "Oh yeah, that. I was going to fix that years ago but there always seemed be more important things to do"? Arcane Jill (confused)
Jul 27 2004
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.
Jul 27 2004
Berin Loritsch wrote:Arcane Jill wrote:Oops, I forgot to mention that most processors also have a BOF (Branch on Overflow). This is the reason that the overflow flag is involved. This info dates back to when I did assembly work (not so much anymore).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
Jul 27 2004
Berin Loritsch schrieb:Question: what is the difference between a signed shift and an unsigned shift?To fill up the "empty" bit which appears when shifting right, unsigned shift inserts a zero there. Signed shift replicates the old leftmost bit. If you're puzzled, look up "2's complement". Unsigned shift right, is just that - a shift, which appears to divide a positive integer by 2 (or a power thereof). Signed shift right divides by 2 (or a power thereof) taking the sign into attention. With left shifts, there is no difference between signed and unsigned - always fill up with zero, and always correspond to multiplication by 2 (or a power thereof). -eye
Jul 27 2004
Ilya Minkov wrote:Berin Loritsch schrieb:Ok, Signed SHift == Roll Over, and Unsigned Shift == shiftQuestion: what is the difference between a signed shift and an unsigned shift?To fill up the "empty" bit which appears when shifting right, unsigned shift inserts a zero there. Signed shift replicates the old leftmost bit. If you're puzzled, look up "2's complement". Unsigned shift right, is just that - a shift, which appears to divide a positive integer by 2 (or a power thereof). Signed shift right divides by 2 (or a power thereof) taking the sign into attention. With left shifts, there is no difference between signed and unsigned - always fill up with zero, and always correspond to multiplication by 2 (or a power thereof).
Jul 27 2004
Berin Loritsch schrieb:Ok, Signed SHift == Roll Over, and Unsigned Shift == shiftNo. 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
Jul 27 2004
In article <ce5lrj$26js$1 digitaldaemon.com>, Berin Loritsch says...Arcane Jill wrote:I would have expected you to know this, coming from a Java background. Java clearly has both, as documented at Sun's Java website: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html (x >> 1) is the same thing as (x / 2) if x is signed (x >>> 1) is the same thing as (x / 2) if x is unsignedThe 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?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.That's not what it says on the Java web site. Anyway, if >>> does rotate-right, then why is there no corresponding <<< for rotate-left?Most folks know that the shift function is a Q&D multiply/divide by 2.Q and D?The roll over is different, and used in hash functions among other things.Of course, you can use the ROL and ROR instructions in D using inline assembler. But this has got nothing to do with >>> (so I'm changing the thread title). It might be useful to have these functions in std.intrinsic though. Jill
Jul 27 2004
Arcane Jill wrote:In article <ce5lrj$26js$1 digitaldaemon.com>, Berin Loritsch says...Honestly, I have always parsed this in light of my old assembly experience, and really don't get hopped up on reading specs.Arcane Jill wrote:I would have expected you to know this, coming from a Java background. Java clearly has both, as documented at Sun's Java website: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html (x >> 1) is the same thing as (x / 2) if x is signed (x >>> 1) is the same thing as (x / 2) if x is unsignedThe 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?That's not what it says on the Java web site. Anyway, if >>> does rotate-right, then why is there no corresponding <<< for rotate-left?Good questionQuick and DirtyMost folks know that the shift function is a Q&D multiply/divide by 2.Q and D?Right.The roll over is different, and used in hash functions among other things.Of course, you can use the ROL and ROR instructions in D using inline assembler. But this has got nothing to do with >>> (so I'm changing the thread title). It might be useful to have these functions in std.intrinsic though.
Jul 27 2004
On Tue, 27 Jul 2004 09:37:56 -0400, Berin Loritsch wrote:Arcane Jill wrote: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 AMThe 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.
Jul 27 2004
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: 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
Jul 28 2004
On Wed, 28 Jul 2004 07:24:25 +0000 (UTC), Arcane Jill wrote:In article <ce6qk5$2ohe$1 digitaldaemon.com>, Derek Parnell says...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 PMI 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: 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).
Jul 28 2004
Arcane Jill wrote:The docs say: "<< is a left shift. >> is a signed right shift. >>> is an unsigned right shift." Now, here's the thing: We don't have separate operators for signed multiply and unsigned multiply. Why not? Because one operator does both jobs, based on the type of its arguments. We don't have separate operators for signed divide and unsigned divide. Why not? Because one operator does both jobs, based on the type of its arguments. Same goes for %. And yet, we DO have separater operators for signed right shift and unsigned right shift. C doesn't. C++ doesn't. But D does. ? And, if I have understood the documentation correctly, these operators the actual sign of the arguments (which means that D's (uint >> uint) will behave differently from C). I believe that Java has both >> and >>> - but with good reason. Java has no unsigned integers, and so you need a separate operator (>>>) to do unsigned shift right. This logic does not apply to D. Is there a point to all this? Or is it just a case of "Oh yeah, that. I was going to fix that years ago but there always seemed be more important things to do"? Arcane Jill (confused)Maybe the operator was introduced because Java has it?? In Java >>> operator makes sense, since Java has only signed types, but you are right: The >>> operator doesn't make much sense in D.
Jul 27 2004
In article <ce5jsr$260p$1 digitaldaemon.com>, Arcane Jill says...Is there a point to all this?http://www.digitalmars.com/d/ctod.html#ushr ? -- Kaz.
Jul 27 2004
In article <ce5ubo$2bhe$1 digitaldaemon.com>, Kazuhiro Inaba says...In article <ce5jsr$260p$1 digitaldaemon.com>, Arcane Jill says...Aha - I missed that. Thanks for pointing that out. It does make perfect sense now. Well, I suppose the point is that the docs say: "<< is a left shift. >> is a signed right shift. >>> is an unsigned right shift." Which means that the docs are wrong, and this is in fact a documentation error. So the phrase ">> is a signed right shift" should read ">> is a right shift, which is signed if the left hand argument is signed, or unsigned if the left hand argument is unsigned". JillIs there a point to all this?http://www.digitalmars.com/d/ctod.html#ushr ? -- Kaz.
Jul 27 2004