I have learned cast. Because I was not sure about the details of the cast.

source code

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class CastTest {
@Test
public void testDoubleToInt() {
for (int i = 0; i < 1e5; i++) {
double a = randomInt((int) -1e9, (int) 1e9);
assertEquals((int) a, Math.floor(a), 0);
}
{
double a = Integer.MAX_VALUE;
a *= 2;
assertEquals((int) a, Integer.MAX_VALUE);
a *= -1;
assertEquals((int) a, Integer.MIN_VALUE);
}
}

private static final int randomInt(int min, int max) {
return min + (int) (((max + 1) - min) * Math.random());
}

@Test
public void testLongToInt() {
int size = Integer.SIZE;
final long mask = (1L << size) - 1;
long a = 0;
for (int i = 0; i < 1e5; i++) {
a ^= (1L << (int) (64 * Math.random()));
long cast = (a & (1L << (size - 1))) == 0 ? a & mask : a | flip(mask);
assertEquals((int) a, cast);
assertEquals(toBinaryString((int) a), toBinaryString(a).substring(64 - size, 64));
}
}

@Test
public void testLongToShort() {
int size = Short.SIZE;
final long mask = (1L << size) - 1;
long a = 0;
for (int i = 0; i < 1e5; i++) {
a ^= (1L << (int) (64 * Math.random()));
long cast = (a & (1L << (size - 1))) == 0 ? a & mask : a | flip(mask);
assertEquals((short) a, cast);
assertEquals(toBinaryString((short) a), toBinaryString(a).substring(64 - size, 64));
}
}

@Test
public void testLongToByte() {
int size = Byte.SIZE;
final long mask = (1L << size) - 1;
long a = 0;
for (int i = 0; i < 1e5; i++) {
a ^= (1L << (int) (64 * Math.random()));
long cast = (a & (1L << (size - 1))) == 0 ? a & mask : a | flip(mask);
assertEquals((byte) a, cast);
assertEquals(toBinaryString((byte) a), toBinaryString(a).substring(64 - size, 64));
}
}

private static final long flip(long mask) {
return -1L ^ mask;
}

private static final String toBinaryString(byte a) {
StringBuilder binary = new StringBuilder();
for (int i = 7; i >= 0; i--) {
binary.append(((a >> i) & 1) == 0 ? "0" : "1");
}
return binary.toString();
}

private static final String toBinaryString(short a) {
StringBuilder binary = new StringBuilder();
for (int i = 15; i >= 0; i--) {
binary.append(((a >> i) & 1) == 0 ? "0" : "1");
}
return binary.toString();
}

private static final String toBinaryString(int a) {
StringBuilder binary = new StringBuilder();
for (int i = 31; i >= 0; i--) {
binary.append(((a >> i) & 1) == 0 ? "0" : "1");
}
return binary.toString();
}

private static final String toBinaryString(long a) {
StringBuilder binary = new StringBuilder();
for (int i = 63; i >= 0; i--) {
binary.append(((a >> i) & 1L) == 0 ? "0" : "1");
}
return binary.toString();
}
}