Skip to main content

Is (x*256) equivalent with (x

4 replies [Last post]
linuxhippy
Offline
Joined: 2004-01-07

Hi there,

I have a piece of encrytion code in my framework which is used quite a lot.

When looking for tuning opportuinities I replaced int*256 quite often with int<<8 and performance is a lot better.

I wonder wether this is corrent in all cases or wether there are corner cases where this is not allowed?

Thank you in advance, lg Clemens

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jarouch
Offline
Joined: 2004-03-04

Yes, it is, but I am realy surprised, that such a simple optimization is not done by compiler/JVM...

rasbold
Offline
Joined: 2005-06-21

As far as Hotspot goes, both the client and server compilers do perform this transformation.

It is possible, however, that some complexity in the program may confuse the issue for the JIT. It is really impossible to tell what the problem is without running program in the JVM itself.

linuxhippy
Offline
Joined: 2004-01-07

Sorry for the confusion, I simply got the wrong numbers here. I modified my java code to use the shift operation everywhere and got exactly the same numbers ... although in some situations there seem to be differences.

However Hotspot-Server is so fast I don't need to care at all :-)

lg Clemens

rasbold
Offline
Joined: 2005-06-21

Yes, the two are equivalent for int types.

Which VM are you using?

Message was edited by: rasbold