• 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

plz clear me plz i m waiting

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
i m faheem and preparing java1.4exam
i m very confuse abt this code plz help me how can it complile and what happan behind it;
class A3{
public static void main(String args[]){
int a,b;
a=10;
System.out.println("The value of a is "+a);
char c='32';
System.out.println("The value of character is "+c);
byte tiny=(byte)775; (<------------)
System.out.println("The value of tiny is " +tiny);
}
}
look at this line
byte tiny=(byte)775; (<------------)
System.out.println("The value of tiny is " +tiny);
what happan this
i know abt byte its width is 255;
but we r cast it in "775"
and it is complie and give answer is 7 on this line
and abt one more if we cast it in it 255 it give them a answer 1
and if i put it in 256 it give 0
i m also confuse abt it
is it's width is 256 not 255
and i think it is count 0 also with counting 0 it is not possilbe plz help me
am i right or not
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte is a signed integer number -128 to 127. Key to remember is that signed numbers use their high-order bit to represent the sign. So you set it by casting 755 into the byte.
Example Class:

---------- run ----------
Byte Range: -128 to 127
755 binary = 1011110011
b = -13 binary = 11110011
Output completed (0 sec consumed) - Normal Termination
[ February 20, 2003: Message edited by: Tom Adams ]
 
bhatti mansha
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need further explanation
can any body explain me abt high order bit and low order bit.
what is the participation of high order bit inthis program.
faheem
 
Tom Adams
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Binary represetation of integers is a long subject. I glanced at the JLS but couldn't find much - but I'm not real good at finding stuff in it.
I found this link which should get you going...
Binary Representation
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bhatti,
In order to understand cast integer value to byte value, which is biger than the range between -128 to 127, you should understand binary representation for the bit pattern of byte.
Okay, byte has the binary range of -2^7 to (2^7)-1,
Your code above has integer value 755, i don't know what the binary value of 755(you could calculater or you could keep divide 775 by 2.
when you cast 755 to byte value, you are losing the most significant bits from the left to the right 32 bit in integer representation to 8 bit representation in byte.
for example, if you have 32 bit representation of
0000 0000 0000 1100 1000 1110 1001 0000
when you cast to integer to short value(16bit). what you guess?
1000 1110 1001 0000
above 32 bit representation is plus some big number, but you can have negative number in short!
and what if you cast to byte,
1001 0000
you get UNUSAL negative numbers in byte.
Am i right?

In byte first bit is sign bit, 1 is minus 0 is plus,
hope that it help.
the above example, to me, it look like the code test the UNSUAL CASE when you cast integer to byte without thinking about the binary bit representation.
[ February 21, 2003: Message edited by: stephen Kang ]
reply
    Bookmark Topic Watch Topic
  • New Topic