This week's book giveaway is in the Other Languages forum.We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# java 128 bit string value and left shift

J amodi
Greenhorn
Posts: 18
Hi Guys,

I have two problems in general. Please reply asap.

1. I have 128 bit string value.. something like "100000000000000000001111111000000000000000011111100000000000000000000" upto 128 bit long, and I need to do left shift onto it. How would I do that? The problem is java does not take any value which is this much long and if I try to divide this string and then do left shift, answer will be different.

2. Again, if I have a number like 10010, and if I apply left shift on it , it should give me answer like 00101 and not 100100. How would I do this ?

Thanks.
Codeeater

Jeff Verdegan
Bartender
Posts: 6109
6
J amodi wrote:if I try to divide this string and then do left shift, answer will be different.

Not if you do it right. Note, however, that BigDecimal has shift methods.

2. Again, if I have a number like 10010, and if I apply left shift on it , it should give me answer like 00101 and not 100100. How would I do this ?

What you describe is a rotate, not a shift.

Henry Wong
author
Marshal
Posts: 21504
84

If. as the title states, the data is a "string" value, then isn't shifting just creating a new string (with the left side chopped off and a new character appended to the right)? This problem is about processing the string, and not really about any shifting of bits.

Henry

J amodi
Greenhorn
Posts: 18
Hi Jeff,

You are right, Bigdecimal does help me. For the second question, how would i perform rotate operation on BigInteger value ?

Hi Henry,

I am suppose to do binary operation. I am having i/p as hex which i am converting in binary string. then i need to do other operations as mentioned above. I need to do bit rotate operation in Biginteger. Do you have anny idea?

Thanks.
Codeeater

Koen Aerts
Ranch Hand
Posts: 344
BitInteger already contains methods for bit shifting.

J amodi
Greenhorn
Posts: 18
Hi,

BigInteger contains a method for shifting, not the rotation.

Thanks.

Koen Aerts
Ranch Hand
Posts: 344
Could this not be achieved by using some of the other bit-related methods from BigInteger, for instance read the most left bit, then shift, then set the most right bit to the one read before the shift?

J amodi
Greenhorn
Posts: 18
Hi,

I also thought of doing the same, but I am not sure how to do such a code.

Thanks.
Codeeater

Koen Aerts
Ranch Hand
Posts: 344
Also, you mentioned you need to do other operations after you rotate the bits. Could you not just take the original string, then remove the most left character, append it to the right, and then convert it to BigDecimal or BigInteger to do the other calculations?