d标的位集测试

/**
 * std::bitset example.
 *
 * Build with:
 *   $ ldc2 -L-lstdc++ bitset.d
 */

modmap (C++) "bitset";

import std.stdio;
import (C++) std.bitset;

void main()
{
    enum : ulong { A=0, B, C, D, E, F, G, H, numColors };
    auto usedColors = new bitset!(numColors);

    usedColors.set(A, true);
    usedColors.set(C, true);

    writeln("usedColors.len=\t", numColors);   // '8'位
    write("usedColors = \t");
    if (usedColors.any())// any() 工作
    {
        for(int i=0; i<usedColors.size; i++)   // size seems to work
            if (usedColors.test(i))            // works
                write('1');
            else
                write('0');
        write('\n');// 对usedColors,打印10100000
    }               // seems backwards also see 'b' below
                    // is this right?

    writeln("C bit = \t", usedColors.test(C));      // true as it should be
    writeln("count = \t", usedColors.count());      // seems like count is
                                                    // working...returns 2
    writeln("as ulong = \t", usedColors.to_ulong);  // '5' is correct

    writeln("all = \t\t", usedColors.all);
    writeln("none = \t\t", usedColors.none);        // these work
    writeln("any = \t\t", usedColors.any);

    usedColors.flip(C);
    writeln("C flipped = \t", usedColors.test(C));  // false as appropriate

    write("b = \t\t");
    auto a = new bitset!(4u)(0b0110);
    auto b = new bitset!(4u)(0b1101);
    for(int i=0; i<b.size; i++)        // size seems to work
    {
        if (b.test(i))
            write('1'); // prints out 1011 for 'b'
        else
            write('0');
    }
    write('\n');
    writeln("b as ulong = \t", b.to_ulong); // '13' is correct

// FAILURE in phobos format.d
//    writeln(b);

// FAILURE because the [] operator isn't recognised
//    writeln(usedColors[C]);

// FAILURE on operators again
//    auto d = a&b;

}
posted @   zjh6  阅读(8)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示