Forums Register Login

Rotating Bits

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thank you soooooo much!! That worked perfectly. You're a binary god!
[ August 04, 2006: Message edited by: Dion Adler ]
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3670 times.
Similar Threads
Q about bitwise operators
bits manipulation
shifting bits of -1
Beginner question about bit conversion
Questions on MIN_VALUE
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:43:56.