0

1 byte = 8bits

I converted 1111 1111 binary number to decimal .it is giving me 255.

But when i converted 0111 1111 binaru number to decimal .it is giving me 127.

So on what basis the range is declared.Please help me.

Thanks in advance...

PSR
  • 36,137
  • 33
  • 104
  • 147

2 Answers2

4

The number types in Java are signed, meaning they can be negative or positive. The leftmost bit (the most significant bit) is used to represent the sign, where a 1 means negative and 0 means positive.

Byte

Max 01111111  = +127
Min 10000000  = -128

    11111111  = -1

Short

Max 0111111111111111  = +32767
Min 1000000000000000  = -32768

    0000000011111111  = +255

Binary negative numbers are represented in 2's complement form.

Paul Vargas
  • 38,878
  • 15
  • 91
  • 139
2

One bit is reserved for detrmining whether number is negative or positive .

So for max postive number value will be

 01111111 which gives the int number as 128(leftmost bit 0 represent its a postive number)

        64+32+6+8+4+2+1= 127

for max negativenumber value will ((leftmost bit 1 represent its a negative number))

  10000000 which gives the int number as -128

  -128+0+0+0+0+0+0+0 = -128

so range becomes from

  -127 to 128
M Sach
  • 30,322
  • 72
  • 198
  • 300