#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    16
    Rep Power
    0

    How to right shift bitset ? Is it easy?


    hi all,
    i have a bitset say BitSet temp = new BitSet(1000);
    I like to shift it to the right by 1,2...location and fill 0 to the left
    For example
    MSB : 110100 LSB
    After shift it to right 2 bit, we will have
    MSB : 001101 LSB

    in C++, we can do it as temp >>= 2

    How we do this in java?thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    England
    Posts
    952
    Rep Power
    14
    To shift right filling with 0 from the left, use >>>
    Code:
    int i = 8;
    i >>>= 2;
    System.out.println(Integer.toBinaryString(i));
    To fill with the sign bit you would need >>.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    16
    Rep Power
    0
    you shifted the int....not the bitset
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    England
    Posts
    952
    Rep Power
    14
    It doesn't look like BitSet has any method to shift: http://java.sun.com/j2se/1.4.2/docs/...il/BitSet.html

    You could do it using a for loop:
    Code:
    BitSet temp = new BitSet(1000);
    ...
    // shift right one position
    for (int i=0; i<1000; i++)
        temp.set(i, temp.get(i+1));
    temp.clear(1000);

IMN logo majestic logo threadwatch logo seochat tools logo