www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Porting Java code to D that uses << and >>> operators

reply bachmeier <no spam.net> writes:
I'm porting a small piece of Java code into D, but I've run into 
this:

int y1 = ((x12 & MASK12) << 22) + (x12 >>> 9) + ((x13 & MASK13) 
<< 7) + (x13 >>> 24);

I have a basic understanding of those operators in both 
languages, but I can't find a sufficiently detailed explanation 
to tell me how to translate the Java into D and know that it's 
guaranteed to give the same result. Can someone tell me the 
correct D code to use?
May 01 2017
next sibling parent reply Basile B. <b2.temp gmx.com> writes:
On Monday, 1 May 2017 at 15:45:00 UTC, bachmeier wrote:
 I'm porting a small piece of Java code into D, but I've run 
 into this:

 int y1 = ((x12 & MASK12) << 22) + (x12 >>> 9) + ((x13 & MASK13) 
 << 7) + (x13 >>> 24);

 I have a basic understanding of those operators in both 
 languages, but I can't find a sufficiently detailed explanation 
 to tell me how to translate the Java into D and know that it's 
 guaranteed to give the same result. Can someone tell me the 
 correct D code to use?
It's the same code in D. It extracts consecutive bits in x12 and x13 (and maskxx), put them at the beginning (right shift) and add them.
May 01 2017
next sibling parent bachmeier <no spam.net> writes:
On Monday, 1 May 2017 at 15:53:41 UTC, Basile B. wrote:

 It's the same code in D. It extracts consecutive bits in x12 
 and x13 (and maskxx), put them at the beginning (right shift) 
 and add them.
Thanks.
May 01 2017
prev sibling parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
On Monday, 1 May 2017 at 15:53:41 UTC, Basile B. wrote:
 It's the same code in D. It extracts consecutive bits in x12 
 and x13 (and maskxx), put them at the beginning (right shift) 
 and add them.
Reminds me... was the unsigned shift >>> ever fixed?
May 01 2017
parent reply bachmeier <no spam.net> writes:
On Monday, 1 May 2017 at 18:16:48 UTC, Era Scarecrow wrote:
 On Monday, 1 May 2017 at 15:53:41 UTC, Basile B. wrote:
 It's the same code in D. It extracts consecutive bits in x12 
 and x13 (and maskxx), put them at the beginning (right shift) 
 and add them.
Reminds me... was the unsigned shift >>> ever fixed?
What was wrong with it?
May 01 2017
parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Monday, 1 May 2017 at 21:04:15 UTC, bachmeier wrote:
 On Monday, 1 May 2017 at 18:16:48 UTC, Era Scarecrow wrote:
  Reminds me... was the unsigned shift >>> ever fixed?
What was wrong with it?
Doing a broad test I'm seeing an issue with short & byte versions... Course that's probably due to the default upcasting to int rather than short/byte, while the >>>= works just fine. So... byte f0 >> fffffff8 byte f0 >>> 7ffffff8 short f000 >> fffff800 short f000 >>> 7ffff800
May 01 2017
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2017-05-01 17:45, bachmeier wrote:
 I'm porting a small piece of Java code into D, but I've run into this:

 int y1 = ((x12 & MASK12) << 22) + (x12 >>> 9) + ((x13 & MASK13) << 7) +
 (x13 >>> 24);

 I have a basic understanding of those operators in both languages, but I
 can't find a sufficiently detailed explanation to tell me how to
 translate the Java into D and know that it's guaranteed to give the same
 result. Can someone tell me the correct D code to use?
Not sure if this is still the case. But this [1] suggests that D doesn't have an evaluation order defined but Java does. [1] http://dsource.org/projects/dwt/wiki/Porting#Evaluationorder -- /Jacob Carlborg
May 01 2017
next sibling parent bachmeier <no spam.net> writes:
On Monday, 1 May 2017 at 19:06:36 UTC, Jacob Carlborg wrote:

 Not sure if this is still the case. But this [1] suggests that 
 D doesn't have an evaluation order defined but Java does.

 [1] http://dsource.org/projects/dwt/wiki/Porting#Evaluationorder
That's good to know but shouldn't be an issue for this code.
May 01 2017
prev sibling parent reply Faux Amis <faux amis.com> writes:
 
 Not sure if this is still the case. But this [1] suggests that D doesn't 
 have an evaluation order defined but Java does.
 
 [1] http://dsource.org/projects/dwt/wiki/Porting#Evaluationorder
 
To me, this [2] suggests otherwise ;) Or am I missing something? [2] https://dlang.org/spec/expression.html#order-of-evaluation
May 01 2017
parent reply Jacob Carlborg <doob me.com> writes:
On 2017-05-02 01:27, Faux Amis wrote:

 To me, this [2] suggests otherwise ;)
 Or am I missing something?

 [2] https://dlang.org/spec/expression.html#order-of-evaluation
From that link: "Note that dmd currently does not comply with left to right evaluation of function arguments and AssignExpression". -- /Jacob Carlborg
May 02 2017
next sibling parent reply TheGag96 <thegag96 gmail.com> writes:
On Tuesday, 2 May 2017 at 07:42:45 UTC, Jacob Carlborg wrote:
 From that link:

 "Note that dmd currently does not comply with left to right 
 evaluation of function arguments and AssignExpression".
This is something I've never understood. Why doesn't DMD implement the behavior their own language reference specifies? It seems like a very nice guarantee to have...
May 02 2017
parent Faux Amis <faux amis.com> writes:
On 2017-05-02 18:55, TheGag96 wrote:
 On Tuesday, 2 May 2017 at 07:42:45 UTC, Jacob Carlborg wrote:
 From that link:

 "Note that dmd currently does not comply with left to right evaluation 
 of function arguments and AssignExpression".
This is something I've never understood. Why doesn't DMD implement the behavior their own language reference specifies? It seems like a very nice guarantee to have...
https://github.com/dlang/dmd/pull/4035
May 02 2017
prev sibling parent Faux Amis <faux amis.com> writes:
On 2017-05-02 09:42, Jacob Carlborg wrote:
 On 2017-05-02 01:27, Faux Amis wrote:
 
 To me, this [2] suggests otherwise ;)
 Or am I missing something?

 [2] https://dlang.org/spec/expression.html#order-of-evaluation
From that link: "Note that dmd currently does not comply with left to right evaluation of function arguments and AssignExpression".
Yeah, I am blind. Do all the compilers have this problem?
May 02 2017