Skip to content

Instantly share code, notes, and snippets.

@kayru
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayru/327ad61b698f09642ab6 to your computer and use it in GitHub Desktop.
Save kayru/327ad61b698f09642ab6 to your computer and use it in GitHub Desktop.
void fun()
{
union Test1
{
struct
{
u32 foo : 2;
u32 bar : 2;
};
u32 packed;
};
Test1 a, b;
a.foo = 0; a.bar = 1;
b.foo = 2; b.bar = 3;
a.bar = b.bar; // only 'bar' bits are copied
assert(a.foo == 0); // works as expected
union Test2
{
BitField<0, 2, u32> foo;
BitField<2, 2, u32> bar;
u32 packed;
};
Test2 c, d;
c.foo = 0; c.bar = 1;
d.foo = 2; d.bar = 3;
c.bar = d.bar; // entire 'c' is overwritten by 'd' because bitfield types match and custom operator is not called
assert(c.foo == 0); // oops
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment