programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

byte data type

Ranch Hand
Posts: 33
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.

Ranch Hand
Posts: 679

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

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 ?

Ranch Hand
Posts: 198
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

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.

Java Cowboy
Sheriff
Posts: 16083
88
See Two's complement for more details.