Reputation: 639
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
Reputation: 1
Let's calculate range for 1 Byte in C Langauge Datatype char
Upvotes: 0
Reputation: 1519
Let's calculate range for 1 Byte
Upvotes: 14
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
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
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
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
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
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
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
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