Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

Bitwise rotation

Thread view: 
Carles Company Soler - 09 Mar 2005 09:52 GMT
Hello,
does java have a bitwise rotation operator for Integers?

Thanks!

Carles
Daniel Tryba - 09 Mar 2005 11:14 GMT
> does java have a bitwise rotation operator for Integers?

Yes.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html
Eric Sosman - 09 Mar 2005 15:35 GMT
>>does java have a bitwise rotation operator for Integers?
                          ^^^^^^^^              ^^^^^^^^

> Yes.

   No.

   To rotate (not merely shift) an int (not an Integer)
you need an expression something like

    (i << n) | (i >>> (32 - n))

Signature

Eric.Sosman@sun.com

Daniel Tryba - 10 Mar 2005 00:19 GMT
>>>does java have a bitwise rotation operator for Integers?
>                           ^^^^^^^^              ^^^^^^^^
[quoted text clipped - 7 lines]
>
>        (i << n) | (i >>> (32 - n))

I stand correced, mis interpreted due to out of coffee error :)
Daniel Sjöblom - 11 Mar 2005 15:45 GMT
>>>does java have a bitwise rotation operator for Integers?
>
[quoted text clipped - 4 lines]
>
>     (i << n) | (i >>> (32 - n))

Not true in java 1.5, since there are ror/rol operations in
java.lang.Math or Integer, I can't recall which right now. Of course,
they are fairly disappointing (from a performance point of view) since
they are just wrappers that do just the above, although I suspect they
will be compiler intrinsics in later VM versions.

Signature

Daniel Sjöblom
Remove _NOSPAM to reply by mail

Thomas G. Marshall - 14 Mar 2005 15:38 GMT
Eric Sosman coughed up:

>>> does java have a bitwise rotation operator for Integers?
>                           ^^^^^^^^              ^^^^^^^^
[quoted text clipped - 7 lines]
>
> (i << n) | (i >>> (32 - n))

For some reason, this ROR seems much more readable to me:

       (i << (32-n)) | (i >>> n)

I think because it deals with the left most bit first, and I think left to
right.  "Left" is highbit, because of the way you write down binary on
paper.

Signature

Iamamanofconstantsorrow,I'veseentroubleallmydays.Ibidfarewelltoold
Kentucky,TheplacewhereIwasbornandraised.ForsixlongyearsI'vebeenin
trouble,NopleasureshereonearthIfound.ForinthisworldI'mboundtoramble,
Ihavenofriendstohelpmenow....MaybeyourfriendsthinkI'mjustastrangerMyface,
you'llneverseenomore.ButthereisonepromisethatisgivenI'llmeetyouonGod's
goldenshore.

Eric Sosman - 14 Mar 2005 17:19 GMT
> Eric Sosman coughed up:
>
[quoted text clipped - 18 lines]
> right.  "Left" is highbit, because of the way you write down binary on
> paper.

   That's one of the reasons I wrote "something like" ;-)
My mental picture of a rotation is "shift, then replace the
bits that fell off the end."  De gustibus non disputandum est.

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.