• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

java 128 bit string value and left shift

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BitInteger already contains methods for bit shifting.
 
J amodi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

BigInteger contains a method for shifting, not the rotation.

Thanks.
 
Koen Aerts
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Destroy anything that stands in your way. Except this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic