// 30_30-13.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <bitset>

using namespace std;

void disp(bitset<32> &b)
{
	for (int i = (int)b.size()-1; i >= 0; i--) {
		cout << b[i];
		if (i%4 == 0) {
			cout <<  ' ';
		}
	}
	cout << '\n';
}

int _tmain(int argc, _TCHAR* argv[])
{
	unsigned long n;
	string ss = "00010010001101000101011001111000";

	bitset<40> b1;
	bitset<32> b2;
	bitset<32> b3(0x1234abcdUL);
	bitset<32> b4 = 0x1234abdUL;
	bitset<32> b5(ss);
	bitset<32> b6(ss, 4, 16);
	bitset<32> b7((string)"00010010001101000101011001111000");

	// 直接表示
	cout << b1 << '\n';
	cout << b1.to_string() << '\n';

	// 4ビットずつ区切って表示
	disp(b2);
	disp(b3);
	disp(b4);
	disp(b5);
	disp(b6);
	disp(b7);

	// 0xffffを設定
	b2 = 0xffff;
	disp(b2);

	// 1設定と0設定をする
	b2[31] = 1;
	b2.set(30);
	b2[1] = 0;
	b2.reset(0);
	disp(b2);

	// 全ビットを反転
	b2.flip();
	disp(b2);

	// オブジェクトを丸コピー
	b3 = b2;
	disp(b3);

	// 1であるビットの数
	cout << b2.count() << '\n';

	// 各種のテスト
	cout << b2.test(28) << '\n';
	cout << b2.any() << '\n';
	cout << b2.none() << '\n';

	// unsigned longに変換
	n = b2.to_ulong();
	cout << n << '\n';


	MessageBox(NULL, _T("Check Console Window"), L"MessageBox", MB_OK);

	return 0;
}

