Bug 8474 – bitfields doesn't work with 32 bit fields
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-07-30T11:01:00Z
Last change time
2013-10-05T08:18:20Z
Assigned to
rtcvb32
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2012-07-30T11:01:59Z
import std.bitmanip;
struct Foo
{
mixin(bitfields!(
uint, "bits1", 32,
));
}
void main() { }
std\bitmanip.d(76): Error: shift by 32 is outside the range 0..31
Comment #1 by monarchdodra — 2012-07-31T04:40:13Z
FYI: Bitfileds DO work with fields over 32. They just don't work for fields that are EXACTLY 32.
The problem is that since the field is 32 bits long, it is placed in a uint.
When trying to create the "signed mask" though the "1" is attempted to be placed at index 32 (1 << 32), which becomes illegal.
The problem does not appear if the field is SMALLER than 32, since the mask will fit.
The problem does not appear if the field is BIGGER than 32, the type will be placed in a ulong.
Oh yeah, and fields that are exactly 64 bits long don't work either. This is arguably less of a problem though... but still wrong.
=>Changing title
Comment #2 by monarchdodra — 2012-07-31T09:49:40Z
More details: The problem only appears if the 32 (64) bit field is aligned with 0.
THIS will not create a bug:
--------
import std.bitmanip;
struct A
{
mixin(bitfields!(
uint, "a", 1,
uint, "b", 32,
uint, "c", 31,)
);
}
void main()
{
A a;
};