반응형
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
31
32
33
34
35
36
37
38
39
40 |
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
int main()
{
unsigned char data = 182;
int bit_loc = 0;
unsigned char bit_result = 0;
while (1) {
char str[100];
_itoa_s(data, str, 2);
system("cls");
printf("%s", str);
printf("\n몇번 비트를 확인 할 까요 ? : ");
scanf_s("%d", &bit_loc);
bit_result = data >> bit_loc;
bit_result = bit_result & 0x01;
if (bit_result) {
data = data & (~(1 << bit_loc));
}
else {
data = data | (1 << bit_loc);
}
// if문 수식을 3항 연산자로 간결하게 할 수 있다.
//data = (bit_result == 1) ? data & (~(1 << bit_loc)) : data | (1 << bit_loc);
}
return 0;
}
|
cs |
반응형
'Study > C' 카테고리의 다른 글
C언어 - 비트연산3 (0을 이동시키기) (0) | 2018.08.29 |
---|---|
C언어 - 비트연산2 (1을 이동시키기) (0) | 2018.08.29 |
C언어 - Tictactoe 게임 (0) | 2018.08.28 |
C언어 - 블럭 옴기기 게임 (0) | 2018.08.28 |
C언어 - 배열 포인터 (0) | 2018.08.28 |