0

I have declared 3 different types of byte arrays(of different sizes). See the comment next to each as I am not able to understand how the length is computed by the compiler?

    byte[] byteField0 = new byte[2^3];
    System.out.println("bitField0 " + byteField0.length); // Gives 1 byte instead of 8?
    byte[] byteField2 = new byte[2^5];
    System.out.println("byteField2 " + byteField2.length); // Gives 7 bytes instead of 32?
    byte[] byteField3 = new byte[8];
    System.out.println("bitField3: " + byteField3.length); // Gives 8 bytes as expected
curiousengineer
  • 1,678
  • 4
  • 30
  • 52

1 Answers1

2

This has nothing to do with array size. Print those numbers individually, or more importantly, as binary.

^ is XOR bitwise operator, not a replacement for Math.pow

Or as mentioned in comments, powers of two can be accomplished with a different bitwise operator, the left shift <<, which would be computationally faster than Math.pow

OneCricketeer
  • 151,199
  • 17
  • 111
  • 216