• Post Reply Bookmark Topic Watch Topic
  • New Topic

byte data type  RSS feed

 
Popescu Ion
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I was wondering about byte data type. It is 8-bit based, one bit for sign and other 7 for the number itself, therefor maximum 7 bit number is 1111111 = 127, why then byte range is -128 -> 127. How -128 is represented in memory if max possible 7-bit number is 127. Thank You.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Popescu Ion wrote:It is 8-bit based, one bit for sign and other 7 for the number itself

You seem to have answered your own question
 
Popescu Ion
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian Burkett wrote:You seem to have answered your own question

I mean -128 number is represented as 10000000, it needs 8 bit + 1 bit (sign) = 9 bit to represent -128. For the range of -127 -> 127 8-bit it is enough, but beginning with -128 it needs 9 bit. Why ?
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
integers are stored in 2's complement form, and most right bit represents the sign as well as value. So, If you are having value like 1111 1011 then it will be calculated as below.

1111 1011 = -128 + 64 + 32 + 16 + 8 + 0 + 2 + 1 = (-2^7 + 2^6 + ...) = -5

for -128 its stored as 10000000 so the value is calculated as -128.

 
Popescu Ion
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Kumar Jain wrote:integers are stored in 2's complement form, and most right bit represents the sign as well as value. So, If you are having value like 1111 1011 then it will be calculated as below.

1111 1011 = -128 + 64 + 32 + 16 + 8 + 0 + 2 + 1 = (-2^7 + 2^6 + ...) = -5

for -128 its stored as 10000000 so the value is calculated as -128.



Now I understand, thank you.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Two's complement for more details.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!