=================================
=================================
=================================
출처: http://blog.acidraincity.com/2014/07/java-bit-option.html
설명은 코드와 주석으로 대신합니다.
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
public class BitTest{ /** * 옵션상태중 특정 옵션을 활성화시킵니다. * @param options 옵션상태 * @param enable 활성화할 옵션 * @return 활성화 적용된 옵션상태 * @category */ public static int optionEnable( int options, int enable ){ return options | enable; } //method /** * 옵션상태중 특정 옵션을 비활성화시킵니다. * @param options 옵션상태 * @param disable 비활성화할 옵션 * @return 비활성화 적용된 옵션상태 * @category */ public static int optionDisable( int options, int disable ){ return options & ~disable; } //method /** * 옵션상태에 대해 해당 옵션이 활성화되어있는지 확인합니다. * @param options 옵션상태 * @param check 체크할 옵션 * @return 활성화 여부 * @category */ public static boolean isOptionEnabled( int options, int check ){ return ( options & check ) != 0 ; } //method public static final int option1 = 0x00000001 ; public static final int option2 = 0x00000002 ; public static final int option3 = 0x00000004 ; public static final int option4 = 0x00000008 ; public static final int option5 = 0x00000010 ; public static final int option6 = 0x00000020 ; public static final int option7 = 0x00000040 ; public static final int option8 = 0x00000080 ; public static final int option9 = 0x00000100 ; public static void main( String[] args ){ //각 비트가 1로 셋팅된 상태가 //해당 옵션의 활성화 상태를 의미합니다. //라는걸 보여주기 위한 출력코드 System.out.println( Integer.toBinaryString( option1 ) ); //output : 1 System.out.println( Integer.toBinaryString( option2 ) ); //output : 10 System.out.println( Integer.toBinaryString( option3 ) ); //output : 100 System.out.println( Integer.toBinaryString( option4 ) ); //output : 1000 System.out.println( Integer.toBinaryString( option5 ) ); //output : 10000 System.out.println( Integer.toBinaryString( option6 ) ); //output : 100000 System.out.println( Integer.toBinaryString( option7 ) ); //output : 1000000 System.out.println( Integer.toBinaryString( option8 ) ); //output : 10000000 System.out.println( Integer.toBinaryString( option9 ) ); //output : 100000000 //아래처럼 사용합니다. int options = option1 | option3; System.out.println( isOptionEnabled( options, option1 ) ); //output : true System.out.println( isOptionEnabled( options, option3 ) ); //output : true System.out.println( isOptionEnabled( options, option7 ) ); //output : false options = optionEnable( options, option7 ); System.out.println( isOptionEnabled( options, option7 ) ); //output : true options = optionDisable( options, option1 ); System.out.println( isOptionEnabled( options, option1 ) ); //output : false } //method } //class |
=================================
=================================
=================================
출처: http://jaram.tistory.com/13
윈도우에 보면 항상 윈도우 스타일을 설정 할때 | 를 이용하여 여러개의
bit값을 or를 하게 된다.
예를 들어 아래와 처럼 말이다.
WS_POPUP | WS_CHILD | WS_ ... 이런식으로 BIT OR 연산으로 플래그를 사용한다.
어떤 기능을 수행하는 함수를 만들때 여러가지 옵션에 따라서 수행 방법을 달리 하거나 할때 유용하다.
PlatForm API를 만들때 종종 사용이 된다. 위에서도 언급 했듯이 윈도우 스타일 ~ Popup 스타일등
그런 옵션을 만드는 방법은?
이것은 몇개 없을경우(1,2개 정도)에는 괜찮지만 많아 지면 각각의 옵션마다
전부다 if를 해야하는 관계로 무척 복잡하다.
초급용.
이것은 하나의 변수로 여러 옵션을 만들 경우 유용하다.
그러나 #define을 잘 쓰지 않으면 몇번이 어떤 옵션인지 헷갈린다.
중급용.
반드시 #define과 함께 쓰고 비트 연산을 한다.
무척 유용하다. 윈도우에서 자주 쓰는 스타일이다.
고급용이다.
1,2 번은 대충 짐작을 할것이고
3번에 대해서 알아보자.
2번의 경우 각각을 if 하는데 어떤 기능인지 헷갈린다고 했다.
3번 같은 플래그는 일단.
기능별로 분리를 하고 #define을 해야한다.
#define FLAG_AAA 0x001
#define FLAG_BBB 0x002
#define FLAG_CCC 0x004
#define FLAG_DDD 0x008
#define FLAG_EEE 0x010
이런식으로 정의 한다.
우리가 함수에서 플래그를 넣을때
dwFlags = FLAG_AAA | FLAG_BBB | FLAG_CCC;
이렇게 넣었다고 가정해보자.
| 는 비트 OR이므로
FLAG_AAA = 00001
FLAG_BBB = 00010
FLAG_CCC = 00100
이렇게 된다. (이진수로)
다 합치면(OR)하면 00111이다.
그러면 그 인자를 받아서 detect해보는 작업을 하겠다.
if( FLAG_BBB & dwFlags )
이렇게 하면 이 값이 true가 된다.
왜냐?
비트 연산을 한번더 해보면
dwFlags는 이진수로 00111이고.
FLAG_BBB는 이진수로 00010이다.(0x002)
& (AND) 연산을 해보자.
AND는 둘다 1일때만 결과가 1이다.
00111
00010
-----
00010 이 결과이다.
이것은 0보다 큰 값이므로 true가 된다.
우리가 처음에 FLAG_AAA | FLAG_BBB | FLAG_CCC를 했으므로
if( FLAG_DDD & dwFlags )를 해보면..
00111
01000
----- &하면
00000
if의 결과는 false이다.
-0- 기억이 가물 가물한데 내가 썼던 글인가????
=================================
=================================
=================================
'프로그래밍 관련 > 프로그래밍 관련팁' 카테고리의 다른 글
AND 연산과 쉬프트 연산을 이용한 추출법, 분할, 조합(마우스, lParam, wParam) 관련 (0) | 2017.05.18 |
---|---|
Visual Studio "비쥬얼시튜디오" 에서 리소스(.RC)파일을 열때 “리소스 컴파일러 DLL을 찾을 수 없습니다” 에러가 발생할 때 관련 (0) | 2017.05.12 |
(웹, 플래시, 자바)관련 코딩 관련 작업하는데 다른곳이나 다른 브라우져에 작업물이 확인했던거와 다르거나 작업물 업데이트(변환)이 느릴때 방법 (0) | 2013.03.14 |
자바스크립트에서 액션스크립트까지 변수 정리 (0) | 2011.04.18 |
iis와 tomcat 연동 (0) | 2011.03.30 |
댓글 영역