-2

1 or 0 is one bit. A byte is 8 bits.

Why a byte can represent any integer from -128 to 127, inclusive, but not from 0 to 255?

Because 11,111,111 in binary is 255, and you include 0, thats 256 integers you can represent in a byte.

Also, my book auhor just tells me it's 256 because 2 to 8th power. ok, it makes sense, but how? what's the math?

  • 2
    Do you understand how signed math works, especially two's complement? – chrylis -cautiouslyoptimistic- Aug 22 '17 at 19:46
  • what is signed math? like 2 and -2? – most venerable sir Aug 22 '17 at 19:47
  • Possible duplicate of [Signed and unsigned data types in java](https://stackoverflow.com/questions/21089540/signed-and-unsigned-data-types-in-java) – Am_I_Helpful Aug 22 '17 at 19:48
  • 1
    There are 256 unique 8-bit values. A "byte" *can* represent an integer from 0 to 255, or a single ASCII character, or anything else that there are 256 of. The Java language designers chose to use integers -128 to 127 for the `byte` datatype. – Blorgbeard Aug 22 '17 at 19:48
  • 2
    8 bits gives 256 different values ; the rest is a matter of convention. – Déjà vu Aug 22 '17 at 19:48
  • byte can code up to 256 values (what they are depends on how you treat bit sequences). In your example byte can represent 256 sequential numbers either from -128 to 127 (256 in total including zero) or from 0 to 255 (256 including zero) – Alexey R. Aug 22 '17 at 19:49
  • Possible duplicate of [Why doesn't Java support unsigned ints?](https://stackoverflow.com/questions/430346/why-doesnt-java-support-unsigned-ints) – Kevin J. Chase Aug 22 '17 at 20:29
  • See also: "[James Gosling's explanation of why Java's byte is signed](https://stackoverflow.com/questions/3108297)". – Kevin J. Chase Aug 22 '17 at 20:30

4 Answers4

4

Each bit can take two values, 0 and 1.

Since you have eight bits, and each can take values independent of the other bits, an 8-bit variable can take 2*2*2*2*2*2*2*2 = 2**8 = 256 distinct values.

It's up to the programmer to decide whether those values come from the [-128; 127] range, or from the [0; 255] range (both contain 256 different values). We call the former "signed 8-bit integers" and the latter "unsigned 8-bit integers".

Signed values are usually represented using two's complement.

Java's byte is an example of an 8-bit signed two's complement integer

but why is it 2 to 8 power? thats the key question. Instead of adding 128, 64, 32, 16, 8, 4, 2, 1

Those numbers that you list are all powers of two: 128=2**7, 64=2**6, 32=2**5, ..., 1=2**0. It is easy to show algebraically that when you add them up you get the next power of two minus one: 128+64+32+16+8+4+2+1=256-1.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

Because byte stores signed values (both positive and negative). If you count from -128 to +127, it is 256 values. Java provides only signed byte values.

Also, it is 2^8 because a byte consist of 8 bits. Each bit can be either '0' or a '1', hence two possible values per bit (2^8).

1 bit -> 2^1 = 2 values 
2 bits -> 2^2 = 4 values
8 bits -> 2^8 = 256 values
n bits -> 2^n.
SJaka
  • 712
  • 13
  • 40
1

You can check ones' complement and two's complement to get better understanding via this link https://en.wikipedia.org/wiki/Signed_number_representations

uğur taş
  • 355
  • 1
  • 6
  • 19
0

Think of it this way: every additional digit doubles the number of possible outputs. If you have 1 digit, there are 2 possible outputs. 2 digits gives you 4 possible outputs. 3 digits gives you 8 possible outputs. So, there are 2^n possible values for a binary number (where n is the number of digits).

Incidentally, this generalizes to other kinds of bases - the formula is b^n (where b is the base and n is the number of digits). So a 3-digit base-10 number has 10^3 possible values, for example. Obviously, binary is just base 2; other popular bases are 8 and 16, but there's no reason we have to limit it to that. We could do, for example, base 7 or base 9 or base 13 or something weird like that, but that's rarely done. (Base 8 is chosen because it's a multiple of 2 and evenly divides 24, which makes it convenient for 24-bit systems; base 16 is convenient for 32-bit and 64-bit machines for a similar reason - it's a lot more concise than binary but it's easier to convert between them).

As others have indicated, there are, in fact, 256 possible values of a byte - it's just that you have to choose whether you want that in the range of -128 to +127 or 0 to 255.