If byte is 8 bit integer then how can we set it to 255?

c#

Solution

Characters are described (in a basic sense) by a numeric representation that fits inside an 8 bit structure. If you look at the ASCII Codes for ascii characters, you'll see that they're related to numbers.

The integer count a bit sequence can represent is generated by the formula 2^n - 1 (as partially described above by @Marc Gravell). So an 8 bit structure can hold 256 values including 0 (also note TCPIP numbers are 4 separate sequences of 8 bit structures). If this was a signed integer, the first bit would be a flag for the sign and the remaining 7 would indicate the value, so while it would still hold 256 values, but the maximum and minimum would be determined by the 7 trailing bits (so 2^7 - 1 = 127).

When you get into Unicode characters and "high ascii" characters, the encoding requires more than an 8 bit structure. So in your example, if you were to assign a byte a value of `76`, a lookup table could be consulted to derive the ascii character `v`.

Problem

The byte keyword denotes an integral type that stores values as indicated in the following table. It's an Unsigned 8-bit integer. If it's only 8 bits then how can we assign it to equal 255? ``` byte myByte = 255; ``` I thought 8 bits was the same thing as just one character?

Original source