• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Bit Wise and Bit Shift error

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends,

I got two methods to make a conversion from byte array to long and the other to make from long to byte array, the problem is that when i use a number with a negative signal the code broken and the result is not the expected, so, if someone can give me a help i'll be thankful so much cause i tried everything but nothing worked

byteArrayToLong:


longToByteArray:



A running that gives a good result:


Now with a negative value that give the erro:

 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the 'Ranch

I'd have a look at the java.nio.*Buffer classes for this kind of conversion.

 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that you are only copying the least significant bytes, which will drop the sign bit most of the time. The solution is to set the sign bit explicitly after you're done copying.
Another tip: Don't use lowercase L as an identifier, or to terminate long literals. It looks dangerously similar to the numeric value 1.
You should call your parameter something like value, and you should use long literals like 65000L.
 
Tiago Cunha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The problem is that you are only copying the least significant bytes, which will drop the sign bit most of the time. The solution is to set the sign bit explicitly after you're done copying.
Another tip: Don't use lowercase L as an identifier, or to terminate long literals. It looks dangerously similar to the numeric value 1.
You should call your parameter something like value, and you should use long literals like 65000L.



Stephan, thanks for the help, i tried what you said and i put this into the end of the method longToByteArray:



but now the return is:


still not working, maybe i made something wrong?
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, what I wrote was from the top of my head, and I didn't test it, so it's likely I made a mistake. I'll quickly do a test and get back to you.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that you're trying to represent -65000 in two bytes. The smallest (signed) value you can represent with two bytes is -32768.

You will have to make clear contracts for your method, what will happen when the value entered can't be represented with the number of bytes for your array. Is your method going to throw an exception? Is it going to lose precision?

Right now it just drops precision. If that's what you want, that's what you have to document, and then it should be no surprise that when you're decoding your byte array, it's going to return a different value from the original.

Don't forget to sign extend your value when you're trying to decode an array that stores a negative number.

Tell us what the purpose of your methods are. Maybe we can suggest easier alternatives. Jelle already suggested Buffers.
 
Tiago Cunha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right Stephan, let me explain the purpose: i got a protocol of communication that specifies some bytes/values.. like this:

bytes[10] (bytes[0..2] is one value that represent one thing, bytes[3..9] represent another thing)... like this, and these 2 methods help me to put a value into a sequence of bytes, like, i want the value "-45" in a byte[] of size 2, so i use the method "longToByteArray(long, 2)" and i receive back this long in a byte[2] so i can put it between bytes[0..2] and bytes[5..9] for example, because when the message be received in the other side the machine will take this bytes[3..4] and convert to a Long using the method byteArrayToLong(bytes, 2, false) for example and must receive the "-45" value...

i tried to use a low value with the modification that you give to me, still not working, like using -45 for example when i run give me this output:


maybe i can change all bits value like all "1" turn "0" and "0" in "1" to change de sign? it can work? please help me.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, ByteBuffers should be pretty useful for this purpose.

Again, this is from the top of my head, I haven't actually tested this. Make sure you read the documentation in ByteBuffer.
 
What are you doing? You are supposed to be reading this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic