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
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# byte value

Ranch Hand
Posts: 88
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
sorry I think I din't answer your question. Was a bit hurry.

Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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 ]

 No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising