• 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

conversion q

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code

byte b = 127;
byte c = 126;
byte a = (byte) (b + c);
What is value for a ?? (-3?) please explain someone.

Question2)
14. We have the fillowing organization of classes.
class Parent { }
class DerivedOne extends Parent { }
class DerivedTwo extends Parent { }
Which of the following statements is correct for the following expression.
p = new Parent ( ) ;
d1 = new DerivedOne ( ) ;
d2 = new DerivedTwo ( ) ;
d1 = (DerivedOne)d2 ;
ans) ilLegal both compile and runtime.
But since we have (DerivedOne) cast, wouldn't work during compile and fail at runtime??
Thank you very much!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>
0111 1111 // 127
0111 1110 // 126
---------
1111 1101 // 1 in sign bit, take as negative (two's complement)
1111 1101 // this is a negative number. To find out its magnitude
0000 0010 // invert all bits to get 1's complement
0000 0001 // then add 1 to get 2's complement
---------
0000 0011 // 3 therefore, 1111 1101 is -3

</pre>
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the second part of your question, casting a DerivedTwo object as a DerivedOne will fail at compile time. This is because the rule that states that one type must be a subclass of the other (doesn't matter which one). Neither d1 or d2 are subclasses of the other.
If you did the following however,
p = d2;
d1 = (DerivedOne)p;
this would compile successfully but generate an exception at runtime. This compiles because it satisfies the rule that one is the subclass of the other (DerivedOne is a subclass of Parent).
However, at runtime, the JVM will check for the actual type of the object referred to by p, which is still a DerivedTwo. Since DerivedTwo is neither a subclass nor a superclass of DerivedOne, an exception is thrown.

[This message has been edited by JUNILU LACAR (edited June 14, 2001).]
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your second question..
Casting between peer classes are not allowed & hence the error.
For casting to work they need to be parent-child related.
How will it clear w/o run time error depends upon whom is being assigned to whom and which class does it refers to during run time.
This is the summary of rules that i follow,
Hope this helps.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by JUNILU LACAR:
[B]<pre>
0111 1111 // 127
0111 1110 // 126
---------
1111 1101 // 1 in sign bit, take as negative (two's complement)
*****
Hi Junilu
Can u pl tell me how 127 and 126 was added up ( I mean whethere 1 and 0 is 1 or 1 and 1 is 1 , How?)
*********
1111 1101 // this is a negative number. To find out its magnitude
0000 0010 // invert all bits to get 1's complement
0000 0001 // then add 1 to get 2's complement
---------
Similarly, here How this 2's complement is arrived? whether 0 and 1 is one etc. ??
Thanks
padmini
0000 0011 // 3 therefore, 1111 1101 is -3



[This message has been edited by padmini Babu (edited July 05, 2001).]

 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In binary, negative numbers are one short of the magnitude of its positive counterpart because of wrapping over. For instance, -1 would be 1111 1111. We know its negative because of its left most sign bit which indicates negative. Next we try to find its magnitude by inverting it to 0000 0000. Plus one we get 0000 0001, which is 1.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi padmini,
to add the 126 + 127 just do it normally, they get promoted to int so answer is 253. Do you know how to convert to binary from dec? if not you should review on web somewhere.
Anyway 253 int value in binary code is:
00000000 00000000 ... 111111101 in the low byte
now since it's truncated to a byte again, we only take those last 8 digits and as you know byte is a signed type, so high bit represents sign and the other 7 bits represent the magnitude. Since high bit in this case is 1 the number is negative. Then you just follow procedure to get magnitude, flip bits and add 1.
11111101 becomes
00000010 then + 1
00000011 this is 3 so the number was -3.
does that make sense? don't bother with adding in binary form if you're unsure how to do it, just work out in decimal and then convert the answer to binary.
cheers.

Rich
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic