Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java 128 bit string value and left shift

 
J amodi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21202
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BitInteger already contains methods for bit shifting.
 
J amodi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

BigInteger contains a method for shifting, not the rotation.

Thanks.
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic