Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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?

like image 740
Alex Gordon Avatar asked Oct 26 '10 17:10

Alex Gordon


People also ask

Why is 1 byte 255 and not 256?

A byte is a group of 8 bits. A bit is the most basic unit and can be either 1 or 0. A byte is not just 8 values between 0 and 1, but 256 (28) different combinations (rather permutations) ranging from 00000000 via e.g. 01010101 to 11111111 . Thus, one byte can represent a decimal number between 0(00) and 255.

Why is a byte 255?

A byte has only 8 bits. A bit is a binary digit. So a byte can hold 2 (binary) ^ 8 numbers ranging from 0 to 2^8-1 = 255. It's the same as asking why a 3 digit decimal number can represent values 0 through 999, which is answered in the same manner (10^3 - 1).

How many bytes are required to store a number between 0 and 255?

bit: a single binary digit, either zero or one. byte: 8 bits, can represent positive numbers from 0 to 255.

Why does binary stop at 255?

In binary, it's all 1's and 0's. So, the step before 256 is all 1's. Therefore, 255 in binary is 11111111. The step before any power of 2 is a string of 1's.


1 Answers

There are 256 different configuration of bits in a byte

0000 0000
0000 0001
0000 0010
...
1111 1111

So can assign a byte a value in the 0-255 range

like image 104
Jesper Fyhr Knudsen Avatar answered Sep 19 '22 15:09

Jesper Fyhr Knudsen