programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# tell me easiest way

Dhanesh Kumar
Greenhorn
Posts: 17
Hi friends
see the following code;

byte b=127 // ok
s.o.p(b)// Ans is 127;

byte b=128 // Compile time error;
So we need to type cast by explicitly

byte b=(byte)128 /// ok
s.o.p(b)// ans is –128

if suppose I will write

byte b=(byte) 540 // ok
s.o.p(b)//Ans is 28

I know the how the 28 has to come. But my way is very difficult to find .
So please any one tell me to find ans above or follows.

byte b=(byte)989;
s.o.p(b) // what is ans and how to find the ans by easiest way

Timmy Marks
Ranch Hand
Posts: 226
Convert to binary and take the 8 least significant bits as signed:

989 decimal = 1111011101 binary. the least significant 8 bits are
11011101 which is -35 decimal.

Upon experimentation, and with absolutely no proof whatsoever, in the cases here, you can continually subtract 256 until you are in the range of -128 to 127. That may be easier if it is correct.
[ May 10, 2005: Message edited by: Timmy Marks ]

deepu Bhalotia
Ranch Hand
Posts: 39
I myself have found the easiest way to remember this. Check it if ok you also can use the same..

byte a=127;
System.out.println(a);
127 will be printed on the Console.

byte a=(byte)128;
System.out.println(a);
-128 will be printed on the Console. since Byte is of size 256 (2^8).
you need to do the following calculation.

128-256=-128.

byte a=(byte)540;
System.out.println(a);
28 will be printed on the Console. For the number greater than 256. First take the modulo of that number Like- 540%256.

Calculation-
540%256=28. Since this number is less than 127. There is no need to deduct it from 256.

byte b=(byte)989;
s.o.p(b) // what is ans and how to find the ans by easiest way
Take the modulo of this number.
989%256=221
Now
221-256=-35 will be printed on the Console..

Check it and rectify me if i am wrong....

Deepak

[ May 10, 2005: Message edited by: deepu Bhalotia ]
[ May 10, 2005: Message edited by: deepu Bhalotia ]

Ashok Kumar
Ranch Hand
Posts: 93
I found a round about way for this.If anybody has an easier way please reply

for
byte b=(byte)256;
sop(b);
we get b=0;

So if x is the number its equivalent byte value is got by the formula

(x%256)(for x>127)

Dhanesh Kumar
Greenhorn
Posts: 17
hi

I continual subtract 128 . but it sometime going to wrong output. That is I assure positive number means it will give negative number…

Timmy Marks
Ranch Hand
Posts: 226
Are you looking for the easiest way in your head/on paper or the easiest way for a computer? I would assert that the computer can do it more easily with the %256 method, but that most people can do it for most smaller numbers with the -256 method. For extremely large numbers, I would recommend converting the lowest 8 bits to binary as the quickest method.

Dhanesh Kumar
Greenhorn
Posts: 17
Yes u r exactly correct
Thanks

 It is sorta covered in the JavaRanch Style Guide.