Friday, August 29, 2014

Type conversion of integers in Java


Let's see how integer type are converted in java internally.


Type conversion of integers in Java:

Converting from int to byte and short >>
Example>
int num=132;
byte b= (byte)num;
short s= (short) num;
------------------------------------------------------------------------------------------------------------

Explanation---
Note :>> in java byte (8-bit), short (16-bit), int (32-bit), long (64-bit) are signed 2's complement Integers,
I.e. these data types stores data in form of 2's complement, because java supports signed    integers and to store the negative values it uses the 2’s complement of their binary representation.

byte b= (byte)num;

0000 0000 0000 0000 0000 0000 1000 0100 => 32 bit int

1000 0100 => 8 bit byte [Rest of 24 bits will be chopped off to make it 8 bit from 32 bit, because 132 will not fit within the range of byte which is -128 to 127]
Here 2's complement will be taken---

0111 1011  (1s compliment)
              +1   (2s complement)
0111 1100
----------------
- 124 (byte)
----------------
*************************************************************************************
short s= (short) num;

0000 0000 1000 0100 => 16 bit short [No loss of bit so result will be same, i.e. 132, because 132 is perfectly fits within the range of short which is -32768 to 32767]
Now  Let, short s1= (short) 32769;
So,
0000 0000 0000 0000 1000 0000 0000 0001  => 32769 (an int value)

1000 0000 0000 0001  => (Rest of the bits which are out of range are chopped off here)
0111 1111 1111 1110 (1's complement)
                                  +1 (2's complement)
----------------------------
0111 1111 1111 1111 => - 32767 (short)
----------------------------


Converting from int (hex) to byte and short >>
Example>
int a=0xf1;
byte b= (byte)a;
------------------------------------------------------------------------------------------------------------

Explanation---
Note :>> in java byte (8-bit), short (16-bit), int (32-bit), long (64-bit) are signed 2's complement Integers,
I.e. these data types stores data in form of 2's complement, because java supports signed    integers and to store the negative values it uses the 2’s complement of their binary representation.

Here, int a=0xf1 , represents hexadecimal representation of int value, which is equal to 241.

So,
0000 0000 0000 0000 0000 0000 1111 0001 => 241 (an int value 32 bit)

1111 0001 => bit byte [Rest of 24 bits will be chopped off to make it 8 bit from 32 bit, because 241 will not fit within the range of byte which is -128 to 127]

0000 1110  (1’s compliment)
              +1   (2’s complement)
0000 1111
----------------
- 15 (byte)
----------------
Remember: 2’s complement will be taken when you convert the int to byte and short, or long to int.

Converting from long (hex) to int >>
Example:
long a= 0xffffffff;
int b= (int)a;
-----------------------------------------------------------------------------------------------------------

Explanation---
Note :>> in java byte (8-bit), short (16-bit), int (32-bit), long (64-bit) are signed 2's complement Integers,
I.e. these data types stores data in form of 2's complement, because java supports signed    integers and to store the negative values it uses the 2’s complement of their binary representation.
Here, long a=0xffffffff, represents hexadecimal representation of long value,which is equal to 4294967281.
So,
0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 1111 1111 >>>> 4294967281 [64 bit long]

In int>> 1111 1111 1111 1111 1111 1111 1111 1111 [Rest of the bit are chopped off to fit the digits in the range of int,which is 2-32 TO 2 31]

0000 0000 0000 0000 0000 0000 0000 0000 -- 1's complement
                                                                                +1  -- 2's complement
0000 0000 0000 0000 0000 0000 0000 0001
---------------------------------------------------------
  -1 (int)
----------------------------------------------------------

No comments:

Post a Comment

Thanks for your comments/Suggestions.