본문 바로가기
공부/개발노트

[C/C++] AND, Shift 연산, OR 연산의 활용

반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bitset>
#include <iostream>
using namespace std;
 
int main(){
    unsigned short color = 0x1234;
    
    unsigned short blue;
    blue = color & 0x001f;
    
    cout << "color = " << bitset<16>(color) << "(" << color << ") \n";
    cout << "blue = " << bitset<16>(blue) << "(" << blue << ") \n";
    
    return 0;
}
cs

AND 연산

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bitset>
#include <iostream>
using namespace std;
 
int main(){
    unsigned short color = 0x1234;
    
    unsigned short green_temp;
    green_temp = color & 0x07e0;
    
    unsigned short green;
    green = green_temp >> 5;
    
    cout << "\tcolor = " << bitset<16>(color) << " (" << color 
        << ") \n";
    cout << "green_temp = " << bitset<16>(green_temp) << " (" << green_temp
        << ") \n";
    cout << "\tgreen = " << bitset<16>(green) << " (" << green
        << ") \n";
    
    return 0;
}
cs

Shift 연산

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
#include <bitset>
#include <iostream>
using namespace std;
 
int main(){
    unsigned short color = 0x1234;
    
    unsigned short color_tmp;
    color_tmp = color & 0x07ff;
    
    unsigned short red = 30;
    
    unsigned short red_tmp;
    red_tmp = red << 11;
    
    unsigned short color_finished;
    color_finished = color_tmp | red_tmp;
    
    cout << "color = " << bitset<16>(color) << " (" <<
    color << ") \n";
    cout << "color_tmp = " << bitset<16>(color_tmp) << " (" <<
    color_tmp << ") \n";
    cout << "red = " << bitset<16>(red) << " (" << red << ") \n";
    cout << "red_tmp = " << bitset<16>(red_tmp) << " (" << red_tmp
    << ") \n";
    cout << "color_finished = " << bitset<16>(color_finished) << color_finished
    << ") \n";
    
    return 0;
}
cs

같이 활용한 예


반응형