• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rotating Bits

 
Dion Adler
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cross post - http://forum.java.sun.com/thread.jspa?threadID=757715&tstart=0
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dion Adler
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you soooooo much!! That worked perfectly. You're a binary god!
[ August 04, 2006: Message edited by: Dion Adler ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic