Results 1 to 6 of 6
  1. #1
    nwboy74 is offline Member
    Join Date
    Oct 2008
    Posts
    45
    Rep Power
    0

    Default Shift Off Trailing Zeroes

    Is there an easy/fast way to shift off trailing zeroes from a binary number? Suppose I have the number 108 (1101100). I'd like to turn that into 27 (11011) as quickly and efficiently as possible. I could run it through a loop until mod 2 is 1, but I wasn't sure if there was some nifty bitwise operation that could be applied to get the same result more efficiently.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by nwboy74 View Post
    Is there an easy/fast way to shift off trailing zeroes from a binary number? Suppose I have the number 108 (1101100). I'd like to turn that into 27 (11011) as quickly and efficiently as possible. I could run it through a loop until mod 2 is 1, but I wasn't sure if there was some nifty bitwise operation that could be applied to get the same result more efficiently.
    The 'best' (mind the quotes) I can come up with is an ugly mixture of Integer.lowestOneBit( ... ) and a switch with 32 case statements ...it is conceptually fast though ...

    kind regards,

    Jos

  3. #3
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    How about:

    Java Code:
    int i = 24;
    i >>= Integer.lowestOneBit(i)/2 - 1;
    System.out.println(i);
    My Hobby Project: LegacyClone

  4. #4
    nwboy74 is offline Member
    Join Date
    Oct 2008
    Posts
    45
    Rep Power
    0

    Default

    That's interesting. However, when I try Integer.lowestOneBit(108), it's giving me 4 rather than 3.

  5. #5
    nwboy74 is offline Member
    Join Date
    Oct 2008
    Posts
    45
    Rep Power
    0

    Default

    Ah. Integer.numberOfTrailingZeros seems to do the trick. Thanks!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by nwboy74 View Post
    Ah. Integer.numberOfTrailingZeros seems to do the trick. Thanks!
    Have you seen the code? Here it is:

    Java Code:
        public static int numberOfTrailingZeros(int i) {
            // HD, Figure 5-14
    	int y;
    	if (i == 0) return 32;
    	int n = 31;
    	y = i <<16; if (y != 0) { n = n -16; i = y; }
    	y = i << 8; if (y != 0) { n = n - 8; i = y; }
    	y = i << 4; if (y != 0) { n = n - 4; i = y; }
    	y = i << 2; if (y != 0) { n = n - 2; i = y; }
    	return n - ((i << 1) >>> 31);
        }
    My crude suggestion of using lowestOneBit() followed by an ugly switch of 32 cases is faster imho.

    kind regards,

    Jos

Similar Threads

  1. Insert and Shift Array
    By topcat1325 in forum New To Java
    Replies: 4
    Last Post: 02-05-2010, 02:50 AM
  2. Anyway to fix the lines so they dun shift?
    By PeterFeng in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 10:26 AM
  3. Run method on Ctrl+Shift+D
    By Echilon in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2008, 10:31 PM
  4. Replies: 1
    Last Post: 07-24-2007, 01:44 AM
  5. Replies: 1
    Last Post: 07-24-2007, 12:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •