aspose file tools*
The moose likes Java in General and the fly likes Rotating Bits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Rotating Bits" Watch "Rotating Bits" New topic
Author

Rotating Bits

Dion Adler
Greenhorn

Joined: Aug 03, 2006
Posts: 3
Hello everyone,

I was just wondering if someone could help me figure out how to write a function that rotates bits in Java while carrying over. For example, if I had :

int test = 0x41; //00000000000000000000000001000001 <-- 65

and I wanted to rotate to the right by 3( could be any number, just an example), then I want the number to turn out to:

00100000000000000000000000001000 <-- 536870920

Using the bit shift operators fills the variable with zeroes when the ones fall off. Any help would be greatly appreciated!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Hi,

Welcome to JavaRanch!

You may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Initials aren't enough. You can change your display name here. Thanks!


[Jess in Action][AskingGoodQuestions]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
Think about how to detect the fact that you are about to rotate a zero or a 1 bit out of the least significant bit. Think about how to place a zero or a 1 bit in the most significant bit.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Cross post - http://forum.java.sun.com/thread.jspa?threadID=757715&tstart=0


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by James Sabre:
Cross post - http://forum.java.sun.com/thread.jspa?threadID=757715&tstart=0


We don't really discourage this. We don't want people to post the same question in more than one forum at JavaRanch, but posting in the Sun forums and also here -- it's OK.
Dion Adler
Greenhorn

Joined: Aug 03, 2006
Posts: 3
Thanks for the help, and being so good about the cross posting. But as you can see, Sabre also posted in my sun forums post, though offered no real help. I don't see what would tempt someone to say someone is posting their hw when they do not know/have never met the person, thus have no idea who they are and what they do. And once again, showing his immaturity he decided to state in this forum that I was cross-posting. I don't know who Sabre is or what I ever did to him to deserve his disdain, but as I stated in the sun forum, if you don't have anything useful to say, don't say anything.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

Since Java 5.0, the class java.lang.Integer has methods rotateLeft(...) and rotateRight(...) that rotate the bits in an integer. Look those methods up in the API documentation.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
If you can't use Java 5, it is not terribly difficult to do rotation.

For the example of rotating a 32-bit integer right by 3...

Take the original number and shift left by 32-3=29 bits. Remember this as A.

Take the original number and shift right by 3 bits. Use the >>> operator, so the top 3 bits are definitely zero after the shift. Call this B.

Your result is A ORed with B.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Dion Adler
Greenhorn

Joined: Aug 03, 2006
Posts: 3
Thank you soooooo much!! That worked perfectly. You're a binary god!
[ August 04, 2006: Message edited by: Dion Adler ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rotating Bits