• 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

tell me easiest way

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes u r exactly correct
Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic