Left shift operator fails on int (not in real Java)

16 views
Skip to first unread message

jin...@tappytaps.com

unread,
Apr 5, 2019, 9:26:50 AM4/5/19
to j2objc-discuss
Hi,

I am implementing custom CRC algorithm and I have problem with bit shifts.

For example, this works in Java:

int i = 214502010;
System.out.println(i << 8);

but not in J2ObjC:

runtime error: left shift of 214502010 by 8 places cannot be represented in type 'jint' (aka 'int')
-922060288

Any workaround for this?

Thanks,

Jindrich

jin...@tappytaps.com

unread,
Apr 5, 2019, 10:09:39 AM4/5/19
to j2objc-discuss
Hi,

I even was not successful with re-implementation as described at JLS spec, section 15.19. It looks like J2ObjC has stronger overflow checking, than Java:

int i = 214502010;
// i << 8
int i2 = i * (int)Math.pow(2, 8);

This works in Java, in j2objc again - overflow error.

However, in JLS specification is written clearly:

The value of n << s is n left-shifted s bit positions; this is equivalent (even if overflow occurs) to multiplication by two to the power s.

- so that it should not fail on overflow.

Jindrich


Dne pátek 5. dubna 2019 15:26:50 UTC+2 jin...@tappytaps.com napsal(a):

Antonio Cortes Perez

unread,
Apr 5, 2019, 11:04:49 AM4/5/19
to j2objc-...@googlegroups.com
This is not a J2ObjC issue. It has to do with the clang flags that you are using. I was able to transpile, compile and run your provided example.


--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages