Question related to range of the primitive datatypes
up vote
0
down vote
favorite
In java, short has range of -128 to 127. Why is it not -127 to 127 ? Considering one bit is used for storing the sign of the number, the negative limit should have been -127 which is sum of 2^6+2^5+2^4+2^3+2^2+2^1+2^0 ? What am I missing in my calculations ?
java primitive
add a comment |
up vote
0
down vote
favorite
In java, short has range of -128 to 127. Why is it not -127 to 127 ? Considering one bit is used for storing the sign of the number, the negative limit should have been -127 which is sum of 2^6+2^5+2^4+2^3+2^2+2^1+2^0 ? What am I missing in my calculations ?
java primitive
java byte has values -128 to 127
– Sharon Ben Asher
Nov 17 at 19:53
Read about how two's complement works. It's not magnitude-with-sign-bit as you suppose.
– chrylis
Nov 17 at 20:04
1
search for Two's Complement which is used by many (most common method) systems/languages to represent positive/negative integers...
– Carlos Heuberger
Nov 17 at 20:08
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In java, short has range of -128 to 127. Why is it not -127 to 127 ? Considering one bit is used for storing the sign of the number, the negative limit should have been -127 which is sum of 2^6+2^5+2^4+2^3+2^2+2^1+2^0 ? What am I missing in my calculations ?
java primitive
In java, short has range of -128 to 127. Why is it not -127 to 127 ? Considering one bit is used for storing the sign of the number, the negative limit should have been -127 which is sum of 2^6+2^5+2^4+2^3+2^2+2^1+2^0 ? What am I missing in my calculations ?
java primitive
java primitive
asked Nov 17 at 19:50
sorenl
71
71
java byte has values -128 to 127
– Sharon Ben Asher
Nov 17 at 19:53
Read about how two's complement works. It's not magnitude-with-sign-bit as you suppose.
– chrylis
Nov 17 at 20:04
1
search for Two's Complement which is used by many (most common method) systems/languages to represent positive/negative integers...
– Carlos Heuberger
Nov 17 at 20:08
add a comment |
java byte has values -128 to 127
– Sharon Ben Asher
Nov 17 at 19:53
Read about how two's complement works. It's not magnitude-with-sign-bit as you suppose.
– chrylis
Nov 17 at 20:04
1
search for Two's Complement which is used by many (most common method) systems/languages to represent positive/negative integers...
– Carlos Heuberger
Nov 17 at 20:08
java byte has values -128 to 127
– Sharon Ben Asher
Nov 17 at 19:53
java byte has values -128 to 127
– Sharon Ben Asher
Nov 17 at 19:53
Read about how two's complement works. It's not magnitude-with-sign-bit as you suppose.
– chrylis
Nov 17 at 20:04
Read about how two's complement works. It's not magnitude-with-sign-bit as you suppose.
– chrylis
Nov 17 at 20:04
1
1
search for Two's Complement which is used by many (most common method) systems/languages to represent positive/negative integers...
– Carlos Heuberger
Nov 17 at 20:08
search for Two's Complement which is used by many (most common method) systems/languages to represent positive/negative integers...
– Carlos Heuberger
Nov 17 at 20:08
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
I will assume you meant byte
rather than short
, as short
has a range of -32768 to 32767.
You appear to be under the impression that, for integer types (including short
), a negative number is represented with a signed bit and the rest of the number as normal. So -1
would be represented as 1000.0001
(dot added for readability). This, however, is incorrect.
In reality, -1
is represented as 1111.1111
, and 1000.0000
is actually -128
(and not -0
, which doesn't exist for this type). This is to keep the arithmetic consistent across all values.
add a comment |
up vote
1
down vote
You are using 8-Bits to store that in the memory of the computer. This would simply 0 to 255. Considering that you also want negative numbers, you must change the way of looking on these numbers.
Let's say that we take the 8th bit as an indicator for negativity. The negative number would be the negation of the positive number. This would let us display numbers from -127 to 127, but there would be two ways to display the 0: 1111 1111 and 0000 0000. This is called One's Complement.
Because engineers wanted to use all the available space, the came up with a new idea. To convert a position number in to a negative ( * - 1) they decided to fully negate the number and increment it by one. This would give them the possibility of using the whole 256 range. That's the Two's Complement. There are 128 negative numbers + 1 Zero + 127 positive numbers, which makes 256 numbers.
but probably the most advantage point is that arithmetic is much simpler
– Carlos Heuberger
Nov 17 at 20:12
add a comment |
up vote
0
down vote
Your calculations are wrong. Total possibilities are 2^8 and that is equal to 256. From -128 to 127 because we count 0 as one of those numbers. 128+128 = 256. But 256 + '0' = 257. So it has to be from -128 to 127.
New contributor
add a comment |
up vote
0
down vote
byte
range in Java is from (2^7)-1
to -2^7
, inclusively. Most significant bit which is left-most bit represents sign of the signed int
types either negative or positive. 2^7
has totally 8-bits in which 0th
bit is to determine the sign. It must be 0
for positives, otherwise 1
.
In other words, negative numbers are represented by 2's complement in Java.
By the way, I consider big-endian
representation in my explanation.
0 1111111
^
represents sign, the remaining ones must be `1` for its max value(positive)
which is equal to `(2^7)-1`.
If we make `-2^7` in which whole bits are `1` even for the left-most one.
1 1111111
^
kind of suggesting that up from Java 9 we have unsigned integers... which I believe is not the case.
– Carlos Heuberger
Nov 17 at 20:18
@CarlosHeuberger ok, I've removed that statement. However, there are some modifications to use int as unsigned in Java 8 and newer versions though it is by means of Integer class.
– snr
Nov 17 at 20:19
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I will assume you meant byte
rather than short
, as short
has a range of -32768 to 32767.
You appear to be under the impression that, for integer types (including short
), a negative number is represented with a signed bit and the rest of the number as normal. So -1
would be represented as 1000.0001
(dot added for readability). This, however, is incorrect.
In reality, -1
is represented as 1111.1111
, and 1000.0000
is actually -128
(and not -0
, which doesn't exist for this type). This is to keep the arithmetic consistent across all values.
add a comment |
up vote
1
down vote
I will assume you meant byte
rather than short
, as short
has a range of -32768 to 32767.
You appear to be under the impression that, for integer types (including short
), a negative number is represented with a signed bit and the rest of the number as normal. So -1
would be represented as 1000.0001
(dot added for readability). This, however, is incorrect.
In reality, -1
is represented as 1111.1111
, and 1000.0000
is actually -128
(and not -0
, which doesn't exist for this type). This is to keep the arithmetic consistent across all values.
add a comment |
up vote
1
down vote
up vote
1
down vote
I will assume you meant byte
rather than short
, as short
has a range of -32768 to 32767.
You appear to be under the impression that, for integer types (including short
), a negative number is represented with a signed bit and the rest of the number as normal. So -1
would be represented as 1000.0001
(dot added for readability). This, however, is incorrect.
In reality, -1
is represented as 1111.1111
, and 1000.0000
is actually -128
(and not -0
, which doesn't exist for this type). This is to keep the arithmetic consistent across all values.
I will assume you meant byte
rather than short
, as short
has a range of -32768 to 32767.
You appear to be under the impression that, for integer types (including short
), a negative number is represented with a signed bit and the rest of the number as normal. So -1
would be represented as 1000.0001
(dot added for readability). This, however, is incorrect.
In reality, -1
is represented as 1111.1111
, and 1000.0000
is actually -128
(and not -0
, which doesn't exist for this type). This is to keep the arithmetic consistent across all values.
answered Nov 17 at 19:56
Joe C
9,81352341
9,81352341
add a comment |
add a comment |
up vote
1
down vote
You are using 8-Bits to store that in the memory of the computer. This would simply 0 to 255. Considering that you also want negative numbers, you must change the way of looking on these numbers.
Let's say that we take the 8th bit as an indicator for negativity. The negative number would be the negation of the positive number. This would let us display numbers from -127 to 127, but there would be two ways to display the 0: 1111 1111 and 0000 0000. This is called One's Complement.
Because engineers wanted to use all the available space, the came up with a new idea. To convert a position number in to a negative ( * - 1) they decided to fully negate the number and increment it by one. This would give them the possibility of using the whole 256 range. That's the Two's Complement. There are 128 negative numbers + 1 Zero + 127 positive numbers, which makes 256 numbers.
but probably the most advantage point is that arithmetic is much simpler
– Carlos Heuberger
Nov 17 at 20:12
add a comment |
up vote
1
down vote
You are using 8-Bits to store that in the memory of the computer. This would simply 0 to 255. Considering that you also want negative numbers, you must change the way of looking on these numbers.
Let's say that we take the 8th bit as an indicator for negativity. The negative number would be the negation of the positive number. This would let us display numbers from -127 to 127, but there would be two ways to display the 0: 1111 1111 and 0000 0000. This is called One's Complement.
Because engineers wanted to use all the available space, the came up with a new idea. To convert a position number in to a negative ( * - 1) they decided to fully negate the number and increment it by one. This would give them the possibility of using the whole 256 range. That's the Two's Complement. There are 128 negative numbers + 1 Zero + 127 positive numbers, which makes 256 numbers.
but probably the most advantage point is that arithmetic is much simpler
– Carlos Heuberger
Nov 17 at 20:12
add a comment |
up vote
1
down vote
up vote
1
down vote
You are using 8-Bits to store that in the memory of the computer. This would simply 0 to 255. Considering that you also want negative numbers, you must change the way of looking on these numbers.
Let's say that we take the 8th bit as an indicator for negativity. The negative number would be the negation of the positive number. This would let us display numbers from -127 to 127, but there would be two ways to display the 0: 1111 1111 and 0000 0000. This is called One's Complement.
Because engineers wanted to use all the available space, the came up with a new idea. To convert a position number in to a negative ( * - 1) they decided to fully negate the number and increment it by one. This would give them the possibility of using the whole 256 range. That's the Two's Complement. There are 128 negative numbers + 1 Zero + 127 positive numbers, which makes 256 numbers.
You are using 8-Bits to store that in the memory of the computer. This would simply 0 to 255. Considering that you also want negative numbers, you must change the way of looking on these numbers.
Let's say that we take the 8th bit as an indicator for negativity. The negative number would be the negation of the positive number. This would let us display numbers from -127 to 127, but there would be two ways to display the 0: 1111 1111 and 0000 0000. This is called One's Complement.
Because engineers wanted to use all the available space, the came up with a new idea. To convert a position number in to a negative ( * - 1) they decided to fully negate the number and increment it by one. This would give them the possibility of using the whole 256 range. That's the Two's Complement. There are 128 negative numbers + 1 Zero + 127 positive numbers, which makes 256 numbers.
answered Nov 17 at 20:07
Alexander Kaschta
14819
14819
but probably the most advantage point is that arithmetic is much simpler
– Carlos Heuberger
Nov 17 at 20:12
add a comment |
but probably the most advantage point is that arithmetic is much simpler
– Carlos Heuberger
Nov 17 at 20:12
but probably the most advantage point is that arithmetic is much simpler
– Carlos Heuberger
Nov 17 at 20:12
but probably the most advantage point is that arithmetic is much simpler
– Carlos Heuberger
Nov 17 at 20:12
add a comment |
up vote
0
down vote
Your calculations are wrong. Total possibilities are 2^8 and that is equal to 256. From -128 to 127 because we count 0 as one of those numbers. 128+128 = 256. But 256 + '0' = 257. So it has to be from -128 to 127.
New contributor
add a comment |
up vote
0
down vote
Your calculations are wrong. Total possibilities are 2^8 and that is equal to 256. From -128 to 127 because we count 0 as one of those numbers. 128+128 = 256. But 256 + '0' = 257. So it has to be from -128 to 127.
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
Your calculations are wrong. Total possibilities are 2^8 and that is equal to 256. From -128 to 127 because we count 0 as one of those numbers. 128+128 = 256. But 256 + '0' = 257. So it has to be from -128 to 127.
New contributor
Your calculations are wrong. Total possibilities are 2^8 and that is equal to 256. From -128 to 127 because we count 0 as one of those numbers. 128+128 = 256. But 256 + '0' = 257. So it has to be from -128 to 127.
New contributor
New contributor
answered Nov 17 at 19:53
Adeel Malik
1
1
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
byte
range in Java is from (2^7)-1
to -2^7
, inclusively. Most significant bit which is left-most bit represents sign of the signed int
types either negative or positive. 2^7
has totally 8-bits in which 0th
bit is to determine the sign. It must be 0
for positives, otherwise 1
.
In other words, negative numbers are represented by 2's complement in Java.
By the way, I consider big-endian
representation in my explanation.
0 1111111
^
represents sign, the remaining ones must be `1` for its max value(positive)
which is equal to `(2^7)-1`.
If we make `-2^7` in which whole bits are `1` even for the left-most one.
1 1111111
^
kind of suggesting that up from Java 9 we have unsigned integers... which I believe is not the case.
– Carlos Heuberger
Nov 17 at 20:18
@CarlosHeuberger ok, I've removed that statement. However, there are some modifications to use int as unsigned in Java 8 and newer versions though it is by means of Integer class.
– snr
Nov 17 at 20:19
add a comment |
up vote
0
down vote
byte
range in Java is from (2^7)-1
to -2^7
, inclusively. Most significant bit which is left-most bit represents sign of the signed int
types either negative or positive. 2^7
has totally 8-bits in which 0th
bit is to determine the sign. It must be 0
for positives, otherwise 1
.
In other words, negative numbers are represented by 2's complement in Java.
By the way, I consider big-endian
representation in my explanation.
0 1111111
^
represents sign, the remaining ones must be `1` for its max value(positive)
which is equal to `(2^7)-1`.
If we make `-2^7` in which whole bits are `1` even for the left-most one.
1 1111111
^
kind of suggesting that up from Java 9 we have unsigned integers... which I believe is not the case.
– Carlos Heuberger
Nov 17 at 20:18
@CarlosHeuberger ok, I've removed that statement. However, there are some modifications to use int as unsigned in Java 8 and newer versions though it is by means of Integer class.
– snr
Nov 17 at 20:19
add a comment |
up vote
0
down vote
up vote
0
down vote
byte
range in Java is from (2^7)-1
to -2^7
, inclusively. Most significant bit which is left-most bit represents sign of the signed int
types either negative or positive. 2^7
has totally 8-bits in which 0th
bit is to determine the sign. It must be 0
for positives, otherwise 1
.
In other words, negative numbers are represented by 2's complement in Java.
By the way, I consider big-endian
representation in my explanation.
0 1111111
^
represents sign, the remaining ones must be `1` for its max value(positive)
which is equal to `(2^7)-1`.
If we make `-2^7` in which whole bits are `1` even for the left-most one.
1 1111111
^
byte
range in Java is from (2^7)-1
to -2^7
, inclusively. Most significant bit which is left-most bit represents sign of the signed int
types either negative or positive. 2^7
has totally 8-bits in which 0th
bit is to determine the sign. It must be 0
for positives, otherwise 1
.
In other words, negative numbers are represented by 2's complement in Java.
By the way, I consider big-endian
representation in my explanation.
0 1111111
^
represents sign, the remaining ones must be `1` for its max value(positive)
which is equal to `(2^7)-1`.
If we make `-2^7` in which whole bits are `1` even for the left-most one.
1 1111111
^
edited Nov 17 at 20:24
answered Nov 17 at 20:03
snr
5,0081438
5,0081438
kind of suggesting that up from Java 9 we have unsigned integers... which I believe is not the case.
– Carlos Heuberger
Nov 17 at 20:18
@CarlosHeuberger ok, I've removed that statement. However, there are some modifications to use int as unsigned in Java 8 and newer versions though it is by means of Integer class.
– snr
Nov 17 at 20:19
add a comment |
kind of suggesting that up from Java 9 we have unsigned integers... which I believe is not the case.
– Carlos Heuberger
Nov 17 at 20:18
@CarlosHeuberger ok, I've removed that statement. However, there are some modifications to use int as unsigned in Java 8 and newer versions though it is by means of Integer class.
– snr
Nov 17 at 20:19
kind of suggesting that up from Java 9 we have unsigned integers... which I believe is not the case.
– Carlos Heuberger
Nov 17 at 20:18
kind of suggesting that up from Java 9 we have unsigned integers... which I believe is not the case.
– Carlos Heuberger
Nov 17 at 20:18
@CarlosHeuberger ok, I've removed that statement. However, there are some modifications to use int as unsigned in Java 8 and newer versions though it is by means of Integer class.
– snr
Nov 17 at 20:19
@CarlosHeuberger ok, I've removed that statement. However, there are some modifications to use int as unsigned in Java 8 and newer versions though it is by means of Integer class.
– snr
Nov 17 at 20:19
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354958%2fquestion-related-to-range-of-the-primitive-datatypes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
java byte has values -128 to 127
– Sharon Ben Asher
Nov 17 at 19:53
Read about how two's complement works. It's not magnitude-with-sign-bit as you suppose.
– chrylis
Nov 17 at 20:04
1
search for Two's Complement which is used by many (most common method) systems/languages to represent positive/negative integers...
– Carlos Heuberger
Nov 17 at 20:08