• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Rotating Bits

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cross post - http://forum.java.sun.com/thread.jspa?threadID=757715&tstart=0
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic