This week's book giveaway is in the Java in General forum. We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line! See this thread for details.
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.
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)
(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 ]