wlav / cppyy

Other
400 stars 41 forks source link

Problems with reading/writing bitfields #57

Open cgdae opened 2 years ago

cgdae commented 2 years ago

When cppyy reads a bitfield it seems to give the wrong value, apparently because it is including all higher bits.

And setting an individual bitfield seems to set the whole bitfield's word value.

This is with cppyy-2.3.0.

Here's a reproducer:

import cppyy
cppyy.cppdef('''
        struct Foo
        {
            unsigned int a : 1;
            unsigned int b : 2;
            unsigned int c : 4;
            unsigned int d : 1;
            unsigned int e : 8;
            unsigned int f :16;

            Foo() : a(1), b(0), c(0), d(0), e(0x33), f(0x5555)
            {}
        };
        ''')
f = cppyy.gbl.Foo();
print( f'a=0x{f.a:x}')
print( f'b=0x{f.b:x}')
print( f'c=0x{f.c:x}')
print( f'd=0x{f.d:x}')
print( f'e=0x{f.e:x}')
print( f'f=0x{f.f:x}')
f.c = 0xf0
print('After setting f.c to 0xf0:')
print( f'a=0x{f.a:x}')
print( f'b=0x{f.b:x}')
print( f'c=0x{f.c:x}')
print( f'd=0x{f.d:x}')
print( f'e=0x{f.e:x}')
print( f'f=0x{f.f:x}')

I'd expect this to output something like:

a=0x1
b=0x0
c=0x0
d=0x0
e=0x33
f=0x5555
After setting f.c to 0xf0:
a=0x1
b=0x0
c=0xf0
d=0x0
e=0x33
f=0x5555

But i get:

a=0x55553301
b=0x55553301
c=0x55553301
d=0x55553301
e=0x555533
f=0x5555
After setting f.c to 0xf0:
a=0xf0
b=0xf0
c=0xf0
d=0xf0
e=0x0
f=0x0

Thanks.

wlav commented 2 years ago

You're the first to ask about bitfields. :) Had to come one day. I'll have a look and see how they can be supported.