aspose file tools*
The moose likes Beginning Java and the fly likes java 128 bit string value and left shift Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java 128 bit string value and left shift" Watch "java 128 bit string value and left shift" New topic
Author

java 128 bit string value and left shift

J amodi
Greenhorn

Joined: Nov 09, 2011
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

Joined: Jan 03, 2004
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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
J amodi
Greenhorn

Joined: Nov 09, 2011
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

Joined: Feb 07, 2012
Posts: 344

BitInteger already contains methods for bit shifting.
J amodi
Greenhorn

Joined: Nov 09, 2011
Posts: 18
Hi,

BigInteger contains a method for shifting, not the rotation.

Thanks.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
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

Joined: Nov 09, 2011
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

Joined: Feb 07, 2012
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java 128 bit string value and left shift