Say you have a byte variable, and you assign it the decimal value of 102. (byte myByteVariable = 102;) Why is that possible? Shouldn't you have to provide an actual 8 bit value instead to avoid confusion? And how do I set a byte value by supplying bits instead?
-
2`102` is an integer, not a decimal. A decimal would be `102M`, and the value `102.0` would be a double. – Matt Johnson-Pint Sep 13 '15 at 21:30
-
`102` (base 10 "decimal" as opposed to "binary" or "hexadecimal") is an 8 bit value. There's no confusion. There are no binary literals in C#. – Blorgbeard Sep 13 '15 at 21:30
-
1Because the compiler lets you type decimal numbers, it is friendly that way. 102 easily fits in a byte, it only requires 7 bits to get encoded. – Hans Passant Sep 13 '15 at 21:35
-
1@MattJohnson I mean decimal as in a base ten number. – Tim Sep 13 '15 at 21:35
-
Keep in mind that you can use hexadecimal too: `byte myByteVariable = 0x66;`. – Matt Johnson-Pint Sep 13 '15 at 21:45
-
try `byte x = 102M;` and you will see its not possible – M.kazem Akhgary Sep 13 '15 at 22:50
-
http://stackoverflow.com/questions/1491550/converting-from-string-bits-to-byte-or-hex – M.kazem Akhgary Sep 13 '15 at 22:57
3 Answers
byte
is a decimal type which represents a decimal number, it doesn't represent a bit field. So 102
is a normal value for it, because it's in the range of byte
values (which is [0;255]). If you want to manipulate the bits, consider using BitArray
or BitVector32
.

- 5,105
- 2
- 22
- 26
And how do I set a byte value by supplying bits instead?
If you are setting the byte value explicitly, you can do so using the prefix 0x
to indicate that you are doing so in hexadecimal, as an encoding for groups of 4 bits. E.g. the hex value of decimal 102
is 0x66
, which is equivalent to 0110 0110
.
But for supplying bits directly, Marc Gravell provides an option here for converting from binary to int using the Convert class, and similarly you can convert the string representation of a binary value into a byte using the Convert.ToByte method as:
byte b = Convert.ToByte("01100110", 2);
-
-
According to [MSDN](https://msdn.microsoft.com/en-us/library/system.convert), "A conversion method exists to convert every base type to every other base type." Not all conversions are supported though and some attempts will throw exceptions. – khargoosh Sep 14 '15 at 00:46
A byte
can store 8 bits (that is values from 0 to 255). A short
stores 16 bits, an int
32, etc. Each integer type in C# simply allows the storage of a wider range of numbers. C# allows you to assign a whole number to any of these types.
In order to set each bit individually, you will need to use bitwise operators.
I actually wrote a library a while ago to handle this, allowing you to set each bit of the byte using data[x]
. It is very similar to the BitArray
class (which I'm not sure if I knew about when I made it)
The main idea of it is:
private byte data;
public void SetBit(int index, bool value)
{
if (value)
data = (byte)(data | (1 << index));
else
data = (byte)(data & ~(1 << index));
}
public bool GetBit(int index)
{
return ((data & (1 << index)) != 0);
}

- 13,999
- 6
- 50
- 90