Replies: 43 comments 3 replies
-
What if you're on a big-endian system? |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h @MaleDong |
Beta Was this translation helpful? Give feedback.
-
@MaleDong |
Beta Was this translation helpful? Give feedback.
-
@lachbaer & @ufcpp : |
Beta Was this translation helpful? Give feedback.
-
@MaleDong It's not so easy but possible. |
Beta Was this translation helpful? Give feedback.
-
Hope Microsoft can also join in this project by doing something like "Suger Syntax". Your example is only a simple one, maybe we also have something ignored……@ufcpp |
Beta Was this translation helpful? Give feedback.
-
@MaleDong |
Beta Was this translation helpful? Give feedback.
-
Bitfields is the only thing I regret of C/C++ is really more easier to reason of a bitmask as a struct of bit / bits that any field a specific name... yes seeing it as an array of bit would be surely better. |
Beta Was this translation helpful? Give feedback.
-
@fanoI :Generally speaking. I think C# or any language's development shouldn't ONLY foucs on "suger syntax" to simplify code writing, but improve or enhance real functions as well. C# is based on .NET. Of course I must admit that it's not aimming at hardwares, but with the raise of some .NET-based system (like Cosmos or SharpOS……), and other embedded systems in .NET. Some binary controls should also be supported in C# or VB.NET, which helps .NET on a higher stair. |
Beta Was this translation helpful? Give feedback.
-
Yes if a sort "Bit" type could exist in .Net will be surely better so the example I have posted in the linked issue could become: struct reg {
bit bit1;
bit bit2;
bit bit3;
bit bit4;
bit bit5;
bit bit6;
bit bit7;
bit bit8;
}; This could surely work: it occupies the same space of a byte... This is another way to represent the same thing instead of a bitfield we have a bit array that could be represented in a byte: bit[] bitNumber = 0b0000_0001 the problem I see is how is this represented? bit bitNum = number[5]; what is bitNum? It is in reality a byte with only 1 bit active? How this type would represent the case in which some fields occupies more than one bit? A bit array would not work as you would need to see the bits contiguous (that is a bit: 2 accepts values from 0 to 3!). https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/IEEE_754_Double_Floating_Point_Format.svg/618px-IEEE_754_Double_Floating_Point_Format.svg.png The proposal of @DerpMcDerp to have a more object oriented solution as the BitField template seems interesting but I'm asking myself how this could be implemented in C#, something similar to Span? I'm a Cosmos developer and as you can imagine when I could at low level you need to do bitmasking in C# with you would prefer to bitfielding having to do this in C# killed a part of myself: https://github.com/CosmosOS/Cosmos/blob/master/source/Cosmos.HAL/TextScreen.cs#L84 |
Beta Was this translation helpful? Give feedback.
-
@fanoI :
|
Beta Was this translation helpful? Give feedback.
-
The ideal thing should be to have the possibility to create types that have particular size in bits than not the canonical one (8, 16, 32, 64).
The exponent is a integer number between Byte and Short (0 - 4095). Maybe the two things can be unified a Bit / bit normally has size 1, but with some attribute (or other? Let's try to use ':' as in C) we can indicate its size, being bit a type surely it could exists in a struct so for our double: struct DoubleView
{
bit sign;
bit exponent: 11;
bit mantissa: 52;
} the important thing is that the size of a struct written in this way will 64 bit so I can convert (unsafely) from and to double. |
Beta Was this translation helpful? Give feedback.
-
@fanoI :Yes,yours is right on floating types. What's more:Since "Bit" is the smallest one (You can image the atom is the basic element for all the physical things), we can define more and more customized numeric types in the form of struct to apply for a range of areas more accurately and to help to get involved in mathematics or scientific problems. But operators like +,-,*,/ or overloadings must be implemented by ourselves. |
Beta Was this translation helpful? Give feedback.
-
I suggest this feature should be realized by Source Generators. For instance: |
Beta Was this translation helpful? Give feedback.
-
@ufcpp : Yes, to implement the "suger syntax", we should directly put that into CTS, CLS and Source Generators;) |
Beta Was this translation helpful? Give feedback.
-
revised |
Beta Was this translation helpful? Give feedback.
-
Good idea to use an enum as "metadata" it makes the declaration yet more compact! One interesting example could be to take a perfect valid double (for example 42.42) and making becoming it NaN changing the exponent to all '1' (here new C# binary representation can help to make clear what we are doing) and the mantissa with any value != 0, after we have "poison" it we can print it again as Double and we should get printed "NaN" 👍 |
Beta Was this translation helpful? Give feedback.
-
https://github.com/ufcpp/BitFields I have implemented "Bit" types. already implemented:
planned:
|
Beta Was this translation helpful? Give feedback.
-
We could do this with just extension indexer #349 #192 We can write namespace for bit. And extend all integer to support indexer for each bit when using that namespace public static BitExt
{
public static bool this[this int i,int index]
{
get; set; // bit shift operation
}
// same for long,short,byte
} |
Beta Was this translation helpful? Give feedback.
-
While in some case could be useful to see a numeric value as "array of bits" this does not solve "elegantly" the case in which you want to see the numeric value as a bit field in which in some cases the fields are more than 1 bit but - at the same time - not one of the predefined numeric values (for example a nibble = 4 bit or 11 bit as in the case of a double exponent part). I'd say that the class @ufcpp is working should solve this three exigences:
|
Beta Was this translation helpful? Give feedback.
-
The source generator has been implemented: https://github.com/ufcpp/BitFields/blob/master/src/BitFieldsAnalyzer/BitFieldsAnalyzer/BitFieldGeneratorCodeFixProvider.cs |
Beta Was this translation helpful? Give feedback.
-
Excellent! I'll test it tonight... Some things I was thinking:
struct DoubleView
{
Bit11 Fraction;
Bit52 Fraction;
Bit1 Sign;
} the attribute on any field [bitfieldsize] will not be needed anymore... but to get at this perfect result we probably need "code generator" so for now the enum approach + code fix is the best we could have Bit1 Sign = true; // same thing of Bit1 Sign = 1 but it is contrary at how .Net numeric values works there is not explict or implicit cast of a Bool to Int32, you should use the Convert class! Never understood this limitation sincerely... What do you think? |
Beta Was this translation helpful? Give feedback.
-
I think these may be useful in some degree, but I don't have enough time. I am satisfied to some extent with the current minimum implementation...
https://gist.github.com/ufcpp/8c6b582e22952e711f1c6ac09e6cff4d
It's 128 bits for the sake of memory alignment. In some case such a struct could be even bigger. // 24 bytes in sequential layout
// 16 bytes in auto layout
struct X
{
byte A;
long B;
byte C;
}
// 16 bytes
struct Y
{
byte A;
byte B;
long C;
} I'm not confident but copy cost of the struct may be not ignorable. |
Beta Was this translation helpful? Give feedback.
-
Yes probably is for this that .Net does not permits explicit or implicit casts to / from to avoid to create "strange" bool which underlying value is 2 that in the end it is neither true or false as you have shown in your gist. Probably the correct thing is to have "safe" methods to do conversion doing unsafe casts would not give the desired results: to have a "real" bitfield CLR support will be needed. |
Beta Was this translation helpful? Give feedback.
-
Discussion here is not exact, but similar... Proposal: Add a BitManipulation class |
Beta Was this translation helpful? Give feedback.
-
Yes but this idea is more intuitive and simple to use! All the bit manipulations happens in the generated classes code so it is more object oriented too... I would really need this having to interop with low level in Cosmos I hate to do all that bitmasking it seems "wrong" too in OO language but on the other hand well create little classes by hand to "emulate" bitfields become annoying soon... |
Beta Was this translation helpful? Give feedback.
-
Would love the inclusion of Bit, for memory saving purposes. It's optimization that computer science guys probably would also really enjoy. Also is there any talk of supporting bit[] with the larger index sizes of perhaps ulong? I ran into memory restrictions doing seives (Eratosthenes) and had to create objects with bitshifting around a value types to adjust bits manually. BitArray and BitVector never worked well enough but did save some space. |
Beta Was this translation helpful? Give feedback.
-
request too, |
Beta Was this translation helpful? Give feedback.
-
It would be nice to see this feature. However, it seems for me, that it suffices to add the ability to read individual bits of a number using Span (if it's easier to add than uint value = 0xFFFFFFFF;
Span<bool> bits = value.AsBigEndianBitSpan(); and extend BitArray value = new BitArray(14, true);
Span<byte> span = value.AsSpan();
span[0] = (byte) 0x00; But I'm not completely sure about the performance of such constructs. And it certainly requires more thinking of how to deal with endianness. |
Beta Was this translation helpful? Give feedback.
-
For scenario 1, the #5497 extensions will make it possible with an extension indexed |
Beta Was this translation helpful? Give feedback.
-
Languages such as C# or VB.NET has really done a great job in many areas of types, features……ect. However, sometimes we may be in trouble with "bit-type" designs. (PLEASE IGNORE THE FOLLOWING EXAMPLE'S SYNTAX ERRORS, FOR WE'RE NOT FOCUSING ON THAT).
1) Bit Dealing with:"Bit" should be an element type for all numeric types
As far as we see now, for a bit's control. We may use the following symbols such as: &, |, !.
However to apply a deximal or heximal number with another one isn't too sense for us.——Just image if you wanna set a deximal number's certain bit(s) to 0 or 1——Let's see an example:
The given example above is to tell you how to set a certain bit to 0. But that's too hard for us to understand directly. In C# or VB.NET 7.0, we can make it look nicer by the following:
However, it should look more beautiful by the following code snippet:
Here the number is made up of 8-bit array, and each array is a number whose type is of "Bit". So we can also turn it to this:
bit bitNum = number[5]; //Just like string is made up of char array:We can directly control with the certain bit to some number
2) "Bit" can be also combined together as an array (Different From "BitArray", I'll name it as "BinaryArray" in fact that implements IEnumerable, IConvertible……ect).
We can do this:
bit[] bitNumber = 0b0000_0001; //We can also say a deximal, heximal number……
byte = (byte)bitNumber;
bitNumer = 5;
Suggestions:
bit[] bitNumber = 0b1111_1111_1111_1111;
byte bNumber = (byte) bitNumber; //This will ONLY keep "1111_1111".
Of course these are my tough ideas, I know that there must be many questions that I've not covered yet. But this will be a milestone in my mind that .NET will be stronger in bit-controlling in both syntax and functions. And this will be nicer for those in "Bit-Dealing" members such as mobile port transportations, some embedded systems……ect.
Beta Was this translation helpful? Give feedback.
All reactions