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

byte value

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
Is there any formula for finding a byte value, if the following example is like this.

1) Short a = new Short("7000"); a.byteValue() = 88 and a.shortValue() % 128 = 88
2) Short a = new Short("6100"); a.byteValue() = -44 and a.shortValue() % 128 = 84
3) Short a = new Short("6200"); a.byteValue() = 56 and a.shortValue() % 128 = 56

I found for some values if i add -128 to the result of "a.shortValue() % 128" i am getting the byte values.
But it is not working in all the cases.
So if anybody knows any formula for this please give a reply.
regards,
sri.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please consider the last 8 bytes when the byte value of any bigger data type is converted.

Ex:
Short s = 257; --> 0000 0001 0000 0001

byte b = (byte)s;
then b = 1;
 
Raghusham Sankargal
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry I think I din't answer your question. Was a bit hurry.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the case of Short a = new Short("6100");

In binary, a 16-bit short value of 6100 is:

0001 0111 1101 0100

Does this help you see why a.byteValue() = -44 and a.shortValue() % 128 = 84?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course there's a formula.

The short (the integral type) is 16 signed bits.

So if s is Short (the wrapper type) we have
s.byteValue() = ( s.shortValue() & 0x00FF );
Let's denote this value so far by X.
So X = s.byteValue() = ( s.shortValue() & 0x00FF );
Then as a second step you want to see what is the
result of X % 128. But as we have the relation
-128 <= X <= 127
then it is pretty obvious that

X % 128 = 0 if and only if X == -128 ( but note that X can never be -128 ! )
X % 128 = X if and only if X != -128

Finally we can summarize this as follows:
for all values of s (being a Short wrapper object) we have:
(1) s.byteValue() % 128 = 0
if the last 8 bits of s (the less significant byte of s )
are 1000 0000 (this being the byte value -128 in decimal)

and

(2) s.byteValue() % 128 = ( s.shortValue() & 0x00FF )
in all other cases (all other cases for the less significant
byte of s).

Hope this helps. Note that the conclusion is that the
result which you are looking for depends only on the less
significant byte of s.
[ February 15, 2006: Message edited by: Peter Petrov ]
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic