• Post Reply Bookmark Topic Watch Topic
  • New Topic

error: possible loss of precision  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write some bit banging code where I'm converting a sequence of bytes to a different sequence of bytes.  I feel kinda stupid for asking such a basic question but I haven't done Java in a year or two.


As you can see from the masks and bit twiddling loss of precision isn't a problem.

What am I doing wrong here?
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think I found it.  Some idiot decided a byte was signed, so instead of shifting a 0 into bit 7 when I shift it propagates the sign bit.  I'm guessing the logical operators promote my byte to an int, then I'm trying to stuff that int into a byte.

Sigh.  So I've got 2 problem.  I've gotta get unsigned bytes, and I've got do byte->int->byte conversions.  Whee! indead
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look closely in the Java® Language Specification and see what happens when you use the bitwise operators. Note particularly the returned type. See whether it says anything about promoting the operands.

Why are you trying arithmetic on bytes? Can you change the location of the cast?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:. . .  Some idiot decided . . .
There is no need to be rude and offensive about the designers of the language.
. . . I've gotta get unsigned bytes . . .
There ain't no such thing as an unsigned byte; the only unsigned primitive type is the char. There is considerable controversy about whether there shou‍ld be a unsigned type, but there isn't. If you really need unsigned arithmetic, use the promotion operator and mask the result with 0x0000_00ff.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo

Can't believe 2 burned 2 hours on that.  Only took 15 minutes to write the bit twiddling code.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "some idiot" thing was a joke that obviously flopped. 

I'm parsing a bitwise protocol.  Fields neither start nor end on bytewise boundaries.  Think MPEG2 streams, I wrote a parser for those 20 years ago in C.  The sample code provided only resembles my real code in that I'm doing logical stuff on bit strings.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:The "some idiot" thing was a joke that obviously flopped. . . .
Don't try that sort of joke on the net. It never works
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!