Union with bits ?

Ali Çehreli acehreli at yahoo.com
Wed Jun 14 22:44:41 UTC 2023


On 6/14/23 15:04, Paul wrote:

 > Question: Why do you say "may be slower than necessary"?  Do you mean
 > compile or runtime or both?

Definitely at compile time because the string that gets mixed-in first 
needs to be generated from your specification. For that, the bitfields() 
function must be compiled and executed at compile-time. Then the 
mixed-in code will be compiled. I remember reading here about lackluster 
compilation performance in that area.

And it may be slow at run time as well depending on certain checks the 
functions perform. For example, you will notice that setting the value 2 
will fail at run time on a 1-bit member. If you would never pass a 2 
value for such a function, than it may be seen as extra cost.

By the way, the string that bitfields() generates can be visualized by 
simply printing it:

     const code =
         bitfields!(
             ubyte, "A", 1,
             ubyte, "B", 1,
             ubyte, "C", 1,
             ubyte, "padding", 5,);
     writeln(code);

The output is not pretty for the human eye but those are basically the 
getters and setters that Adam D Ruppe mentioned.

Ali



More information about the Digitalmars-d-learn mailing list