When I try to print the value of z, it gives 64 as result .Can anyone plz tell why ?? byte x=64; byte y=5; byte z= (byte)(x*y);
Bhushan Jawle
Ranch Hand
Joined: Nov 22, 2001
Posts: 248
posted
0
Ideally with proper data type you should get 320 as answer but as it exceeds the capacity of a byte most significant bits (MSB) in binary representation is chopped of . Amount decimal reprn. of remaining bits is 64. i.e. 101000000 is 320 but if you try to fit that in byte first 10 is chopped of so you get 1000000 which is 64 in dec.
Vijayakumar Arya
Ranch Hand
Joined: Jan 27, 2003
Posts: 76
posted
0
Hi, 64 is getting printed correctly. Let me explain how, z = 64 * 5 = 320 == 0x140 You are doing a type conversion (narrowing) on z, this drops the higher order bytes and takes only upto the bytes specified for the datatype, byte - 1 byte short - 2 bytes int - 4 bytes (byte)0x140 = 0x40 = 64 in decimal, so 64 is getting printed. ---------------------------- vijay