반응형
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 |
같이 활용한 예
반응형
'공부 > 개발노트' 카테고리의 다른 글
[C/C++] if문 사용 예문 (0) | 2016.08.16 |
---|---|
[C/C++] 연산 중 발생한 형변환 (0) | 2016.08.16 |
[C/C++] XOR 연산을 이용해서 a와 b 값을 바꾸기 (0) | 2016.08.16 |
[C/C++] 비트 단위 논리 연산자의 사용 (0) | 2016.08.16 |
[C/C++] 비트 단위 연산자 (0) | 2016.08.16 |