• 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

Internal representation

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,
Please consider the following piece of code
public class myclass
{
public static void main(String args[])
{
byte a = -64;
byte b = 192;
System.out.println("the result is " + a*b);
}
}
My question is as follows:
Both 'a' and 'b' are represented internally as 11000000. When the expression a*b is evaluated both 'a' and 'b' are promoted to 'int' type. How does java know that 'a' should be padded with '1s' and 'b' with '0s' ie how does java correctly represent 'a' and 'b' after promotion as
a = 11111111 11111111 11111111 11000000 -64
b = 00000000 00000000 00000000 11000000 +192
to get the correct result of the expression 'a*b'. Is there a tag or sign bit, other than the ambiguous (at least in this case) '1' in the MSB position ofcourse, somewhere that tells Java that 'a' is negative and 'b' is positive although both are represented internally using the same bit pattern?



------------------
Bos Indicus
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The leftmost bit is the sign bit. When the integral type is converted it will be padded with the value of the sign bit. Your code above is incorrect. It will not even compile. byte b cannot hold a value of 192, -128 to 127 is it's range.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic