0

range of bytes is -128 to 127 why this 128-1 in the positive side. why both side is not equal. why this one is subtracted from the right side.

byte 1 byte Stores whole numbers from -128 to 127

size of bytes in java

Shri Mali
  • 59
  • 4

2 Answers2

5

[-128, 128] contains 257 values but 8 bits can only distinguish 28=256 values.

-128 to -1 is 128 values +
0 is 1 value +
1 to 128 is 128 values

Mike Samuel
  • 109,453
  • 27
  • 204
  • 234
2

That's because you count 0 as well!

1 byte is 8 bits, which is 2^8 i.e 256 numbers.

So we have 128 negative numbers (-1 to -128), a 0, and we left with only 127 positive numbers.

Omri Attiya
  • 2,762
  • 2
  • 13
  • 29