'programming/c++'에 해당되는 글 52건

  1. 2019.04.22 3.9 비트 플래그, 비트 마스크
  2. 2019.04.22 3.8 비트단위 연산자
  3. 2019.04.22 3.7 이진수
  4. 2019.04.22 3.6 논리 연산자

게임 아이템 사용 여부 - 비트 플래그 
(옵션이 많을 때 유용하게 사용됨. 메모리, 속도 효율)

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 Item" << bitset<8>(items_flag) << endl;

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

//item3 on
items_flag |= opt3// |= 비트켜기
cout << "Item3 obtained" << 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;}
else{ cout << "not have item0" << endl;}

<result>

0000 0001
0000 0010
0000 0100
0000 1000

no item 0000 0000
item0 obtained 0000 0001
item3 obtained 0000 1001
item3 lost 0000 0001
not have item1
has item0

색깔 값 추출하기 - 비트 마스크 사용 
(플래그의 비트를 조작하거나 검사할 때 사용)


unsigned int pixel_color = 0xDAA520;

cout << std::bitset<32>(pixel_color) << endl;

unsigned char green = (pixel_color & green_mask) >> 8;
//green값만 추출하기 위해서 오른쪽으로 8칸 미뤄준다. right shift 사용. 

unsigned char blue = pixel_color & blue_mask;

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

<result>
0000 0000 1101 1010 1010 0101 0010 0000
green 1010 0101 165
blue 0010 0000 32


'programming > c++' 카테고리의 다른 글

*4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결  (0) 2019.04.23
4.1 지역 변수, 범위, 지속기간  (0) 2019.04.23
3.8 비트단위 연산자  (0) 2019.04.22
3.7 이진수  (0) 2019.04.22
3.6 논리 연산자  (0) 2019.04.22
Posted by 도이(doi)
,

예전에 메모리가 비쌌음. 
메모리에 주소를 줄 수 있는 최소 단위가 1byte이다. 
비트단위 연산자는 메모리를 절약하고 더 빠르기 때문에 사용하였다. 

비트 단위 연산자는 6개가 있다. 

<< left shift

>> right shift 

~   not

&   and

|     or

  xor 


비트 단위 연산자는 unsigned를 기본으로 사용한다. 


shift는 자리를 한 자리씩 민다. 

'programming > c++' 카테고리의 다른 글

4.1 지역 변수, 범위, 지속기간  (0) 2019.04.23
3.9 비트 플래그, 비트 마스크  (0) 2019.04.22
3.7 이진수  (0) 2019.04.22
3.6 논리 연산자  (0) 2019.04.22
3.5 관계 연산자(부등호)  (0) 2019.04.19
Posted by 도이(doi)
,

3.7 이진수

programming/c++ 2019. 4. 22. 11:51

이진수 


1
10 = 2^1*1 + 2^0*0 = 2
11 = 2^1*1 + 2^0*1 = 3

이진수에서 1 더하는 법 

   1
   11
+   1
---------
 100

2가 되면 자릿수를 올려준다. 


2진수 10진수로 변환하기 

7654 3210
0101 1110

2^7*0 + 2^6*1 + 2^5*0 + 2^4*1 + 2^3*1 + 2^2*1 + 2^1*1 + 2^0*0 

= 128*0 + 64*1 + 32*0 + 16*1 + 8*1 + 4*1 + 2*1 + 1*0

= 64 + 16 + 8 + 4 + 2

= 94


10진수 2진수로 

148 

148 / 2 = 74 r0 ( r = remainder ) 

74  / 2 = 37 r0

37  / 2 = 18 r1

18  / 2 = 9  r0

9    / 2 = 4  r1

4    / 2 = 2  r0

2    / 2 = 1  r0

1    / 2 = 0  r1

1001 0100


음의 정수 표현하는 방법 

-5

1. 우선 5를 이진수로 표현한다. 
0000 0101

2. 보수를 취한다. (complement)
1111 1010

3. 1을 더한다. 
1111 1011


signed(부호 정수) vs unsigned(무부호 정수) 

signed를 이진수로 표현할 때는 맨 앞에 숫자가 음수인지 양수인지를 결정한다.
unsigned를 이진수로 표현할 때는 아니다. 

(signed) int 부호정수/ 음의 정수(negative integer), 0, 양의 정수(positive integer)
unsigned int 무부호 정수/ 0, 양의 정수

'programming > c++' 카테고리의 다른 글

3.9 비트 플래그, 비트 마스크  (0) 2019.04.22
3.8 비트단위 연산자  (0) 2019.04.22
3.6 논리 연산자  (0) 2019.04.22
3.5 관계 연산자(부등호)  (0) 2019.04.19
3.4 sizeof, comma operator, conditional operator  (0) 2019.04.19
Posted by 도이(doi)
,

int x = 2;
int y = 2;

if(x == 1 && y++ == 2)
{

}

cout << y << endl;

<result>
2


&& 논리연산자는 왼쪽의 값이 false일 경우
오른쪽도 false로 해서 수식이 작동되지 않은 것이다. 



드모르간 법칙
Image result for 드모르간 법칙



XOR

false false false
false ture true
true false true
true true false


cpp에서는 XOR이 없어서 
if(x != y) 형태로 사용한다. 



bool v1 = true;
bool v2 = false;
bool v3 = false;

bool r1 = v1 || v2 && v3;
bool r2 = (v1 || v2) && v3;
bool r3 = v1 || (v2 && v3);

cout << r1 << endl;
cout << r2 << endl;

<result>

1
0
1

논리 연산자 &&가 || 보다 우선순위가 높다. 
괄호를 치는 것이 중요하다. 

'programming > c++' 카테고리의 다른 글

3.8 비트단위 연산자  (0) 2019.04.22
3.7 이진수  (0) 2019.04.22
3.5 관계 연산자(부등호)  (0) 2019.04.19
3.4 sizeof, comma operator, conditional operator  (0) 2019.04.19
3.3 증감 연산자  (0) 2019.04.18
Posted by 도이(doi)
,