Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I define a bit in c#?

Tags:

c#

c#-7.0

Why isn't there a bit structure in C#?

like image 235
ScalaMyCapela Avatar asked Dec 09 '08 08:12

ScalaMyCapela


People also ask

Is there a bit type in C?

Generally, the smallest addressable chunk of data in C is a byte. You can not have a pointer to a bit, so you can not declare a variable of 1 bit size. But as Sourav Ghosh already pointed out, you can declare bitfields, where one bit is accessed directly.

Which data type uses only one bit?

Unsigned integers use one bit pattern ( all 0s ) to represent zero and all others to represent positive values. Signed integers use half of the possible bit patterns to represent negative numbers, one pattern to represent zero, and half minus 1 to represent positive values.

What is BitArray in C#?

Methods. And(BitArray) Performs the bitwise AND operation between the elements of the current BitArray object and the corresponding elements in the specified array. The current BitArray object will be modified to store the result of the bitwise AND operation.


1 Answers

What would you want to do with it? Bear in mind that the CLR isn't going to try to pack multiple variables into a byte, so having one on its own would be no more useful than boolean. If you wanted to have a collection of them - well, that's what BitArray is for, as David pointed out.

If we did have a Bit structure, I suspect people would expect multiple Bit variables to be packed efficiently in memory - by not having the type in the first place, we avoid that expectation and lead people towards other solutions such as BitArray.

like image 87
Jon Skeet Avatar answered Oct 05 '22 20:10

Jon Skeet