volatNumbers
volatNumbers

Reputation: 135

How to bitwise shift a binary string in java?

I have a 10 bit binary string and i have to bitwise shift circularly at each iteration. I am so confused and lost doing it. What could be the logic behind it to do easily? If it is a hex number, we can do it by num>>1 or num<<1 but string like "1010101010" should be converted to hex before we apply bitwise shift. I have to apply the bitwise shift circularly 10 times.

Upvotes: 1

Views: 12558

Answers (4)

Edwin Buck
Edwin Buck

Reputation: 70909

Assuming that you are dealing with a String that contains zeros and ones (aka a "binary string"), bitwise shifting is easy.

To "shift left", add a "0" char to the right end for each "left shift".

To "shift right", things are a bit more complicated. Assuming that you are always dealing with a "positive number" in binary, remove a character at the right end for each "right shift". If you wish to do a "sign extended shift right", then you need to check the string's length to see if it is the "maximum length" for the particular encoding of a binary value in 2's complement form, then you need to remove the rightmost character and optionally add a "1" character to the left, provided that the leftmost character is already a "1".

Now, since strings are not constrained in length (while WORDs and DWORDs are) it is not clear whether a >>> operator makes sense for a binary string, or even if 2's complement representation of negative numbers makes sense for a binary string, unless you impose a "limit" on the number of valid characters in the string. That limit would have to be the same for two binary strings (or have promotion rules for the smaller string), if you wanted to do any kind of binary math between two binary strings.

The other option is to just convert the string into an integer and use binary operations on the integer.

Upvotes: 1

user unknown
user unknown

Reputation: 36229

int bits = Integer.parseInt ("010101010", 2);

will transform the String into an int. "Converting to hex before we apply bitwise shift" is nonsense - sorry. Hex is just a way to represent an int (for example). You shift the int value, not it's representation.

Upvotes: 0

Helmuth M.
Helmuth M.

Reputation: 541

There are methods in the Integer class to convert to/from Binary Strings.

    int i = Integer.parseInt("1010101010", 2);
    String shiftedi = Integer.toBinaryString(i<<1);

Edit, above is not circular though, but you can use simple Strings operations:

String in = "1010101010";
String shifted1 = in.substring(1)+in.substring(0,1);

Upvotes: 3

mprivat
mprivat

Reputation: 21902

Generally speak, if you want to do numeric operations, it's best to do it on numbers. So I would suggest converting your string the int (or whatever) it actually represents. Then you can do bit shifting and then back to a string if you need.

If you just want to do text operations, then you can use a StringBuffer and play around with the characters.

StringBuffer s = new StringBuffer("1010101010");
for(int i=0; i<10; i++) {
    char c = s.charAt(0);
    s.append(c);
    s.deleteCharAt(0);
}
String result = s.toString();

But that's just ugly if you're just trying to do math

Upvotes: 1

Related Questions