Search code examples
javaintegershort

Casting too large a value to java short


I have a java program that works with audio data.

I have the following code that takes an array of float between -1 and 1 and casts to short. I have realized that this code could maybe be broken if values outside the range of -1 to 1 are provided.

out = (short) ((data[i])*32768);

In the following code if data[i] == 2.0 , it will be too large for a short to hold. In this case would the cast just take the first 16 bits, whatever they may be, and treat it like a short?


Solution

  • (data[i])*32768 will result smallest type that can hold (e.g. integer here)

    Then this cast: out = (short) ((data[i])*32768) will keep least significant bits of short (16 bits here, included sign bit - most significant bit aka left most bit)

    You can see that if MSB is 1. The casted value will be nagative, else value with positive.

        int i = Short.MAX_VALUE;
        short b = (short) i;
        System.out.println(i +" -> "+ b);
        System.out.println("" + Integer.toBinaryString(0xFFFF & b));
    
        i = Short.MAX_VALUE + 1;
        b = (short) i;
        System.out.println(i +" -> "+ b);
        System.out.println("" + Integer.toBinaryString(0xFFFF & b));
    
        i = Short.MAX_VALUE + Short.MAX_VALUE + Short.MAX_VALUE;
        b = (short) i;
        System.out.println(i +" -> "+ b);
        System.out.println("" + Integer.toBinaryString(0xFFFF & b));
    

    Will print

    32767 -> 32767
    111111111111111
    32768 -> -32768
    1000000000000000
    98301 -> 32765
    111111111111101
    

    See 2nd test, cast value has 1 in MSB