Ankit
Ankit

Reputation: 639

How to calculate the range of primitive data types?

According to docs.oracle.com:-

byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

Byte - 8 bits
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0

128 64 32 16 8 4 2 1)

Adding all these numbers we get a total of 255. Then how's the range which is -128 to 127 calculated. Is it hard-coded somewhere or there's some more technicality to this range?

Any suggestions would be appreciated.

Upvotes: 5

Views: 25485

Answers (10)

Michael Gomes
Michael Gomes

Reputation: 1

Let's calculate range for 1 Byte in C Langauge Datatype char

  1. In case of char dataype lets consider -
  2. 1 Byte = 8 Bits
  3. 2^8 = 256
  4. so we can write 2^8 = 256
  5. In case of unsigned 0 to positive integers only. 4.1 In case of signed
  6. we have 128 numbers for the negative side, 0 (zero) and 127 numbers for the positive side
  7. so the range is -128 0 +127
  8. Here 0 is taking one bit

Upvotes: 0

Jayani Sumudini
Jayani Sumudini

Reputation: 1519

Let's calculate range for 1 Byte

  1. 1 bit can take 0 or 1
  2. 1 Byte = 8 Bits
  3. The first bit is used as a sign ( - or + )
  4. then remaining bits are 7
  5. so we can write 2^7 = 128 different numbers for one sign
  6. we get 0 as a positive sign. then we have 128 numbers for the negative side,127 numbers for the positive side and 0 (zero)
  7. so the range is -128 to 127 including 0

Upvotes: 14

user6235549
user6235549

Reputation:

Formula for Range calculation is : -2^(n-1) to (2^(n-1)-1)

where n = no. of bits of primitive datatype. Example:

For int datatype, n is 32, in short datatype, n is 16 etc.

So, int range will be: -2^(32-1) to (2^(32-1)-1)

By using the same formula Range of byte, short, float and double could be calculated.

Upvotes: 7

Cute Pari
Cute Pari

Reputation: 11

Formula to calculate range in Java
-2(n-1) to +2(n-1)-1
Where n is the number of bit (1 byte= 8 bit)

So, for byte type range would be: -2(8-1) to +2(8-1)-1
or, -2(7) to +2(7)-1
or, -128 to +127

Upvotes: 1

Suganth S
Suganth S

Reputation: 1

Bit consists of 0’s and 1’s.byte normally consists of 8 bits.so the values can be calculated using the general formula which is given below,

no of values data type can have=2^n(2 power n), where n represents no of bits. so the value of byte data type=2^8(i.e 1 byte=8 bits),here n=8 byte value=256

And it should be shared equal on both sides of zero ( half values at negative and half value at positive ). Hence the range for byte is from -128 to 127.

Upvotes: -1

Manpreet Saini
Manpreet Saini

Reputation: 11

The last bit i.e. number 8 we are writing it 2^7 is a sign bit that decides negative or positive sign so it is 2^0 +2^1 +2^2 +2^3 +2^4 +2^5+ 2^6

Upvotes: 1

rahul
rahul

Reputation: 1

Range of data types so now we came to know that how we are calculating the Range of the integer data types.This logic is applicable for all the integer data types.

The full details of all the data types are given below,

S.NO Data Type Bits Ranges values

1 boolean 1 – true or false(1 or 0)

2 byte 8 -128 to 127. 256(2^8)

3 short 16 -32,768 to 32,767 65,536(2^16)

4 int 32 -2^31 to (2^31)-1 2^32

5 long 64 Refer NOTE 2^64

6 float 32 Refer NOTE ———

7 double 64 Refer NOTE ———-

8 char 16 0 to 65,535 65,536(2^16)

Upvotes: -3

Agustin Meriles
Agustin Meriles

Reputation: 4854

That's because the first bit is used for the sign, since the data type is signed.

Please refer to http://en.wikipedia.org/wiki/Signed_number_representations

Since there is no unsigned primitive types in Java (like C or C#), is usual to cast it to a bigger type if you need to "overflow" the boundaries.

Upvotes: 2

LuigiEdlCarno
LuigiEdlCarno

Reputation: 2415

It is a signed type, meaning, it still has a range of 255 (as you have correctly calculated), but it starts at -128. So half the range is below zero, 1 possible number is = (zero) and the remaining 127 are above 0.

The first bit is the sign. (1 - minus, 0 - plus)

Upvotes: 7

rich
rich

Reputation: 413

it results from the standard representation of signed values as binary numbers. For a full description see http://en.wikipedia.org/wiki/Two%27s_complement.

-Rich

Upvotes: 0

Related Questions