-3

Why the number changes from byte to integer or from short to byte to integer?

Here the code:

public class TypeCasting {
    public static void main ( String [] args ) {
        
        short c = 292;
        int d = (byte) c;
        
        System.out.println(d); // 36

    }
}
luk2302
  • 55,258
  • 23
  • 97
  • 137
Jerry
  • 1
  • 2

1 Answers1

1
292 = 0001 0010 0100
36  = 0000 0010 0100

Change 292 from Short to a Byte removes the left-hand bits (the first 4). When you re-cast it to an Int those bits are now lost so you will still only remain with 36.

Detritus
  • 357
  • 1
  • 5
  • 13