digitalmars.D.learn - Manipulating Bits of Any Value Type
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (40/40) Jan 26 2014 Has anybody cooked up some generic functions that extend
- Stanislav Blinov (5/12) Jan 26 2014 All of them? Arbitrary structs too? floating point types? Static
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (12/25) Jan 26 2014 For bit-parallel run-length encoding for efficient serialization
- Stanislav Blinov (5/11) Jan 26 2014 SysTime's representation consists of a long and
- Stanislav Blinov (3/8) Jan 26 2014 OTOH, it seems it does :)
Has anybody cooked up some generic functions that extend `core.bitop` bitmanipulations to work on any value type? Something like bool getBit(T)(in T a, int bitnum); // bt T setBit(T)(in T a, int bitnum); // bts auto ref setBitInPlace(T)(ref T a, int bitnum); I know this is relatively easy to implement so that's why I'm curious to why its not already Phobos. **Update:** Here's my first try at this: /** Returns: $(D bitnum):th Bit Of $(D a). TODO: Why is this already in Phobos? TODO: Make this safe for integers */ bool getBit(T, I)(in T a, I bitnum) trusted pure nothrow if (isIntegral!I) { enum nBits = 8*T.sizeof; static if (nBits == 8) alias I = ubyte; else static if (nBits == 16) alias I = ushort; else static if (nBits == 32) alias I = uint; else static if (nBits == 64) alias I = ulong; immutable xi = (*(cast(I*)&a)); immutable bitMask = ((cast(I)1 << bitnum)); /* debug dln(eltIx, ",", bitnum, ",", xi, ",", bitMask); */ return a & bitMask ? true : false; } alias bt = getBit; My idea to make `getBit` work on all types that have value semantics. That's why I need the cast (I think). Is there a traits to check if a type has value semantics or not? Also is there a trait to check if a type supports a specific operation such as bitwise and `&`? I could always use `__traits(compiles, ...)` but standardization is good. To make it even better I guess I need an explicit overload for T's that support bit manipulations in order to make this variant safe right? In my generic solution above I need the `cast` and that's unsafe. See also: https://stackoverflow.com/questions/21368664/manipulating-bits-of-any-value-type
Jan 26 2014
On Sunday, 26 January 2014 at 20:56:29 UTC, Nordlöw wrote:My idea to make `getBit` work on all types that have value semantics.All of them? Arbitrary structs too? floating point types? Static arrays? Might I ask... why do you need this sort of thing? :)That's why I need the cast (I think). Is there a traits to check if a type has value semantics or not?The closest would be std.traits.hasIndirections.Also is there a trait to check if a type supports a specific operation such as bitwise and `&`? I could always use `__traits(compiles, ...)` but standardization is good.No there isn't.
Jan 26 2014
On Sunday, 26 January 2014 at 23:27:58 UTC, Stanislav Blinov wrote:On Sunday, 26 January 2014 at 20:56:29 UTC, Nordlöw wrote:For bit-parallel run-length encoding for efficient serialization of sets of std.datetime:SysTime structures (16 bytes). I'm trying to differentially pack sets of SysTimes entries more efficiently because for a given directory containing files many of these files often share the majority of their bits. Meta-code: foreach (n; 0..SysTime.sizeof) RLC all n-bits for all SysTimes It is more an interesting experiment :)My idea to make `getBit` work on all types that have value semantics.All of them? Arbitrary structs too? floating point types? Static arrays? Might I ask... why do you need this sort of thing? :)Does this work recursively on containing types?That's why I need the cast (I think). Is there a traits to check if a type has value semantics or not?The closest would be std.traits.hasIndirections.Thx.Also is there a trait to check if a type supports a specific operation such as bitwise and `&`? I could always use `__traits(compiles, ...)` but standardization is good.No there isn't.
Jan 26 2014
On Sunday, 26 January 2014 at 23:37:40 UTC, Nordlöw wrote:SysTime's representation consists of a long and Rebindable!(immutable(TimeZone)). The latter is a struct containing a reference.Might I ask... why do you need this sort of thing? :)For bit-parallel run-length encoding for efficient serialization of sets of std.datetime:SysTime structures (16 bytes).AFAIK no, it just checks immediate fields (if any).The closest would be std.traits.hasIndirections.Does this work recursively on containing types?
Jan 26 2014
On Sunday, 26 January 2014 at 23:54:02 UTC, Stanislav Blinov wrote:On Sunday, 26 January 2014 at 23:37:40 UTC, Nordlöw wrote:OTOH, it seems it does :)AFAIK no, it just checks immediate fields (if any).The closest would be std.traits.hasIndirections.Does this work recursively on containing types?
Jan 26 2014