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 ?










share|improve this question






















  • 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

















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 ?










share|improve this question






















  • 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















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 ?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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




















  • 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














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.






share|improve this answer




























    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.






    share|improve this answer





















    • but probably the most advantage point is that arithmetic is much simpler
      – Carlos Heuberger
      Nov 17 at 20:12




















    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.






    share|improve this answer








    New contributor




    Adeel Malik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      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
      ^





      share|improve this answer























      • 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













      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      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

























      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.






      share|improve this answer

























        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.






        share|improve this answer























          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 17 at 19:56









          Joe C

          9,81352341




          9,81352341
























              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.






              share|improve this answer





















              • but probably the most advantage point is that arithmetic is much simpler
                – Carlos Heuberger
                Nov 17 at 20:12

















              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.






              share|improve this answer





















              • but probably the most advantage point is that arithmetic is much simpler
                – Carlos Heuberger
                Nov 17 at 20:12















              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.






              share|improve this answer












              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              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




















              • 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












              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.






              share|improve this answer








              New contributor




              Adeel Malik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






















                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.






                share|improve this answer








                New contributor




                Adeel Malik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                  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.






                  share|improve this answer








                  New contributor




                  Adeel Malik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  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.







                  share|improve this answer








                  New contributor




                  Adeel Malik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Adeel Malik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered Nov 17 at 19:53









                  Adeel Malik

                  1




                  1




                  New contributor




                  Adeel Malik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Adeel Malik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Adeel Malik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






















                      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
                      ^





                      share|improve this answer























                      • 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

















                      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
                      ^





                      share|improve this answer























                      • 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















                      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
                      ^





                      share|improve this answer














                      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
                      ^






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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




















                      • 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




















                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin