C++

C++ 2일차

Muru 2023. 12. 19. 15:26

문자형 char type

#include <iostream>

int main()
{
	//아스키 코드
	using namespace std;

	char c1(65);
	char c2('A');	//문자 하나: 싱글

	cout << c1 << " " << c2 << " " << int(c1) << " " << int(c2) << endl;

	// c-style casting
	// 강제변환
	cout << (char)65 << endl;	//캐스팅: A
	cout << (int)'A' << endl;	

	// c++ style casting
	// 강제 변환
	cout << char(65) << endl;	//캐스팅: A
	cout << int('A') << endl;

	//나중에 객체지향 등에서 사용
	//기본 타입간의 변환: 컴파일러에게 체크해달라는 개념
	cout << static_cast<char>(65) << endl;
	cout << static_cast<int>('A') << endl;

	char ch(97);
	cout << ch << endl;
	cout << static_cast<int>(ch) << endl;
	cout << ch << endl;

	return 0;
}

#include <iostream>
#include <limits>

int main()
{
	//아스키 코드
	using namespace std;

	char c1(65);

	//cout << sizeof(unsigned char) << endl;
	//cout << (int)std::numeric_limits<unsigned char>::max() << endl;
	//cout << (int)std::numeric_limits<unsigned char>::lowest() << endl;

	cout << int('\t') << endl;
	cout << "\"This is first line \a second line \"";
	//cout << "This is first line " << std::flush;	//줄바꿈 안하고 출력하는 flush
	//cout << "second line";

	wchar_t c;
	char32_t c3;	//이모티콘 같은것들

	return 0;
}

int main()
{
	using namespace std;
	
	//comma operator
	int x = 3;
	int y = 10;
	int z = (++x, ++y);	// ++x, ++y, z = y

	cout << x << " " << y << " " << z << endl;	//4, 11, 11

	return 0;
}

관계연산자

int main()
{
	using namespace std;

	int x, y;
	cin >> x >> y;

	cout << "Your Input values are : " << x << " " << y << endl;

	if (x == y)
		cout << "equal" << endl;
	if (x != y)
		cout << "not equal" << endl;
	if (x > y)
		cout << "x is greater than y" << endl;
	if (x < y)
		cout << "x is less than y" << endl;
	if (x >= y)
		cout << "x is greater than y or equal to y" << endl;
	if (x <= y)
		cout << "x is less than y or equal to y" << endl;

	return 0;
}

 


비트단위연산자

한칸  밀어주기,

 


비트 플래그

#include <iostream>
#include <bitset>

int main()
{
	using namespace std;

	const unsigned char opt0 = 1 << 0;
	const unsigned char opt1 = 1 << 1;
	const unsigned char opt2 = 1 << 2;
	const unsigned char opt3 = 1 << 3;
	//...

	cout << bitset<8>(opt0) << endl;
	cout << bitset<8>(opt1) << endl;
	cout << bitset<8>(opt2) << endl;
	cout << bitset<8>(opt3) << endl;

	unsigned char items_flag = 0;
	cout << "No Items " << std::bitset<8>(items_flag) << endl;

	//비트와이즈 |=, &=

	//item0 on
	items_flag |= opt0;
	cout << "Item0 obtained " << std::bitset<8>(items_flag) << endl;

	//item3 on
	items_flag |= opt3;
	cout << "Item3 obtained " << std::bitset<8>(items_flag) << endl;

	//item3 lost
	items_flag &= ~opt3;
	cout << "Item3 lost " << bitset<8>(items_flag) << endl;

	//has item1 ?
	if (items_flag & opt1)
		cout << "Has item1" << endl;
	else
		cout << "Not have item1" << endl;

	//has item0 ?
	if (items_flag & opt0)
		cout << "Has item0" << endl;

	//obtain item 2, 3 (동시 획득)
	items_flag |= (opt2 | opt3);
	cout << bitset<8>(opt2 | opt3) << endl;
	cout << "Item2, 3 obtained" << std::bitset<8>(items_flag) << endl;

	//lost opt2, obtain opt1
	if ((items_flag & opt2) && !(items_flag & opt1))
	{
		//상태 변경시엔 XOR
		items_flag ^= opt2;
		items_flag ^= opt1;
		//items_flag ^= opt2 | opt1;

		cout << bitset<8>(items_flag) << endl;
	}

	return 0;
}


비트 마스크

 

#include <iostream>
#include <bitset>

int main()
{
	using namespace std;

	//비트마스크
	//마스크값으로 컬러 추출
	const unsigned int red_mask = 0xFF0000;
	const unsigned int green_mask = 0x00FF00;
	const unsigned int blue_mask = 0x0000FF;

	cout << bitset<32>(red_mask) << endl;
	cout << bitset<32>(green_mask) << endl;
	cout << bitset<32>(blue_mask) << endl;

	unsigned int pixel_color = 0xDAA520;
	cout << bitset<32>(pixel_color) << endl;

	unsigned char red = (pixel_color & red_mask) >> 16;
	unsigned char green = (pixel_color & green_mask) >> 8;
	unsigned char blue = pixel_color & blue_mask;

	cout << "red " << bitset<8>(red) << endl;
	cout << "red " << bitset<8>(red) << " " << (int)red << endl;
	cout << "green " << bitset<8>(green) << endl;
	cout << "blue: " << bitset<8>(green) << " " << (int)green << endl;
	cout << "blue: " << bitset<8>(blue) << endl;
	cout << "blue: " << bitset<8>(blue) << " " << (int)blue << endl;

	return 0;
}