File: bit.d

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (106 lines) | stat: -rw-r--r-- 1,499 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* REQUIRED_ARGS: -preview=bitfields
 */

struct T
{
    uint x : 2, y : 3, :0;
    int :0;
}

uint foo(T s)
{
    return s.x + s.y;
}

void test1()
{
    T s;
    s.x = 2;
    s.y = 4;
    uint u = foo(s);
    assert(u == 6);
}

/********************************************/

struct S
{
    uint a:3;
    uint b:1;
    ulong c:64;

    int d:3;
    int e:1;
    long f:64;

    int i;
    alias f this;
}

static assert(S.a.min == 0);
static assert(S.a.max == 7);

static assert(S.b.min == 0);
static assert(S.b.max == 1);

static assert(S.c.min == 0);
static assert(S.c.max == ulong.max);

static assert(S.d.min == -4);
static assert(S.d.max == 3);

static assert(S.e.min == -1);
static assert(S.e.max == 0);

static assert(S.f.min == long.min);
static assert(S.f.max == long.max);
static assert(S.max == S.f.max);

void test2()
{
    int x;
    S effect()
    {
        ++x;
        return S();
    }
    assert(effect().a.max == 7);
    assert(effect().i.max == int.max);
    assert(x == 0); // ensure effect() was not executed
}

/********************************************/

struct U
{
    int a;
    int b:3, c:4;
    this(this)
    {
	b = 2;
    }
}

static assert(U.b.offsetof == 4);
static assert(U.b.sizeof == 4);

void test3()
{
    U u;
    u.c = 4;
    U v = u;
    assert(v.c == 4);
    u = v;
    assert(u.b == 2);
    assert(__traits(getMember, u, "b") == 2);
}

/********************************************/

int main()
{
    test1();
    test2();
    test3();
    return 0;
}