JavaRush /Java Blog /Random EN /Casting primitive types. Casting int to short and byte
Георгий
Level 22
Санкт-Петербург

Casting primitive types. Casting int to short and byte

Published in the Random EN group
Casting primitive types.  Casting int to short and byte - 1Why is it that if you cast some type values int​​to type shortor byte, the results are unexpected? Let's find out!
int i = 450;
byte b = (byte)i;
System.out.println(b);
Result on screen:

-62
Unexpectedly, there is a logical explanation for this; moreover, this action can be done with your own hands. To do this, we need to convert 450 from decimal to binary:

450/2.    0
225/2.    1
112/2.    0
56/2.     0
28/2.     0
14/2.     0
7/2.      1
3/2.      1
1/2.      1
In the right column of the number we write down its remainder when divided by 2, and under the number itself we write the result of dividing our number by two, if the remainder is 0. If the remainder is 1, then below we write the integer part from division by two. ( Online calculator with explanation of calculations ). As a result, we get that in the binary number system 450 = 11100 0010. Any type number intoccupies 4 bytes or 32 bits, where each bit is 0 or 1. In our case, only 9 bits are occupied, and in principle ours int i = 450in binary system looks like this:

0000_0000_0000_0000_0000_0001_1100_0010
We want to write our variable into a variable of type byte, but the type number bytetakes 1 byte (as follows from the name of this type) or 8 bits. Therefore, the extra bits on the left are simply discarded, and in the end we get:

1100 0010
Type value range byte: -128 to 127. Each number occupies 8 bits and the leftmost bit of each number is the sign bit. For all positive numbers it is equal to 0, for all negative numbers it is equal to 1. There is no need to rush to convert our result obtained above into the 10th system, because we received an additional code for the desired number, and not a direct one. The leftmost bit turned out to be equal to 1, therefore our number is negative, and for negative numbers the direct and reverse codes do not coincide, unlike positive ones. If the sign bit were equal to 0, then we could immediately convert the number to the decimal number system and get: 66. But the sign bit is negative, so first we need to convert the additional code into a direct one and add a minus sign to the answer. For clarity and training, first let's try to get the additional code of some number, for example -15. To do this, in the direct code of its positive representation (number 15), you need to change all 0s to 1s and vice versa (get the reverse code, also called inverse), and then add one to the result. In the decimal system 15 = 0000 1111; Reverse code (change all 0s to 1s and vice versa) = 1111 0000; Additional code (add one):

1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 1
1 1 1 1 0 0 0 1
Additional code for the number -15: 1111 0001; Great. Now, by analogy with the example above, we need to convert our additional code into direct code; let me remind you, it is equal to 1100 0010.
  1. Subtract one and get the reverse code. It’s convenient to do this, write down an additional code in the answer and see what you need to add one to in order to get such an additional code. We start with the rightmost digit and look: what do we need to add 1 to to get 0? To 1, we get 10, 0 goes in response, and 1 goes to the next digit. Next, what needs to be added to 0 to get one. One, but since we have a one from the previous digit, we write 0 in response. Next, to get 0, what should we add to 0? Of course, 0. So 4 more times. And there are the last 2 digits left, where you need to add something to 0 to get 1. Of course, in both cases you need to add 1. Total:

    
    1 1 0 0 0 0 0 1
    0 0 0 0 0 0 0 1
    1 1 0 0 0 0 1 0
  2. The hardest part is over! We have received the inverse (reverse) code and all we have to do is get the direct one. We invert all 0 to 1 and vice versa:

    1100 0001- inverse code;

    0011 1110 - the direct code of our number, or rather its positive representation;

  3. Convert to the decimal number system ( Online calculator with explanation of calculations ):

    0011 1110 = 0∙2^7+0∙2^6+1∙2^5+1∙2^4+1∙2^3+1∙2^2+1∙2^1+0∙2^0 = 0+0+32+16+8+4+2+0 = 62;

    All that remains is to add a minus to the number and our answer:-62.

In the same way, numbers of type are converted shortto type int:
int i = 10_000_000;
short s = (short)i;
System.out.println(s); // -27008
  1. 10,000,000 in the 10th number system = 0000 0000 1001 1000 1001 0110 1000 0000in the 2nd number system.

    In Java, a type number inttakes 4 bytes, and a type shortnumber takes 2 bytes, or 16 bits, so we cut off the left to 16 digits:

  2. 1001 0110 1000 0000. The leftmost bit (the most significant bit, also known as the sign bit) turned out to be equal to 1. This means that we have an additional code for a negative number, so we move on to the next point.
  3. Let's translate it into reverse code:

    
    1 0 0 1 0 1 1 0 0 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0

    Return code: 1001 0110 0111 1111;

  4. We invert and get the direct code: 0110 1001 1000 0000.

  5. We convert to the binary number system and get a positive representation of our number:

    1∙2^14+1∙2^13+1∙2^11+1∙2^8+1∙2^7 = 16384+8192+2048+256+128 = 27008.
  6. Add a minus and get the answer:-27008

Casting primitive types.  Casting int to short and byte - 2Link to an online calculator of forward, reverse and complementary codes. Also on this site under the calculator there is a little theory about the inverse and complement code.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION