bit masking
1. ๋นํธ ๋ง์คํน
์ปดํจํฐ๋ ๋ด๋ถ์ ์ผ๋ก ๋ชจ๋ ์๋ฃ๋ฅผ ์ด์ง์๋ก ํํํ๋ค. ์ด๋ฅผ ์ด์ฉํด์ ์ ์์ ์ด์ง์ ํํ์ ์์ ์๋ฃ๊ตฌ์กฐ๋ก ์ฐ๋ ๊ธฐ๋ฒ์ ๋นํธ๋ง์คํฌ๋ผ๊ณ ํ๋ค.
2. ๋นํธ ์ฐ์ฐ์ ์ฐ์ต
/**
* ๋ ์๊ฐ ๋ชจ๋ 1์ผ๋๋ง 1, ๋๋จธ์ง๋ 0
* */
public class AND {
public static void main(String[] args) {
int a = 4; //100
int b = 7; //111
System.out.println(a & b); //100
}
}
/**
* ๋ ์ค ํ๋๋ผ๋ 1์ด๋ฉด 1, ๋๋จธ์ง๋ 0
* */
public class OR {
public static void main(String[] args) {
int a = 2; //010
int b = 5; //101
System.out.println(a | b); //111 = 7
}
}
/**
* ๋ชจ๋ ๋นํธ์ NOT ์ฐ์ฐ์ ํ๋ค.
* */
public class NOT {
public static void main(String[] args) {
int a = 3; //011
System.out.println(~a); //100 = -4
int b = 7; //111 -> 0000 .... 00000111 = 7
System.out.println(~b); //000 -> 1111.... 11111000 = -8
}
}
/*
* ์๋ก ๋ค๋ฅด๋ฉด 1, ๊ฐ๋ค๋ฉด 0
* */
public class XOR {
public static void main(String[] args) {
int a = 3; //011
int b = 5; //101
System.out.println(a ^ b); //110 = 6
}
}
public class Shift {
public static void main(String[] args) {
System.out.println(9 << 2);
//1001 -> 0010 0100 = 4+32 = 36
System.out.println(Integer.toBinaryString(15));
System.out.println(15 << 3);
//1111 -> 0111 1000 = 8+16+32+64 = 24+96 = 120
System.out.println(4 >> 2);
//100 -> 001 = 1
System.out.println(1 << 2);
//001 -> 100 = 4
}
}
3. ์งํฉ
/**
* ์งํฉ
* - ์์๋ฅผ 0 ~ n-1 ๊น์ง ๋ฒํธ๋ฅผ ๋ถ์ฌํ๋ค.
* - ๋ฒํธ์ ํด๋นํ๋ ๋นํธ๊ฐ 1์ด๋ฉด ์์๊ฐ ํฌํจ, 0์ด๋ฉด ๋ถํฌํจ์ด๋ผ๊ณ ํ๋จ
* */
public class Set {
public static void main(String[] args) {
//{A, B, C, D, E, F, G}
//0, 1, 2, 3, 4, 5, 6
/*
* {A} ์ ๋ถ๋ถ์งํฉ = 1000000 = 64
* {C, F}์ ๋ถ๋ถ์งํฉ = 0010010 = 2+16 = 18
* */
}
}
/**
* ์งํฉ์ ์์ ์ถ๊ฐ
* - ํ์ฌ์ํ์ธ cur ์ด ์กด์ฌํ ๋, p๋ฒ ์์๋ฅผ ์ถ๊ฐํ๋ค๊ณ ๊ฐ์ .
* - cur ์์ p๋ฒ์งธ ๋นํธ๋ง 1๋ก ๋ฐ๊พธ์ด์ฃผ์ด์ผ ํ๋ค.
* */
public class SetAdd {
public static void main(String[] args) {
int b = 16; //10000
//๋ชฉํ : 11010 = 26
System.out.println(b | 1 << 3 | 1 << 1);
int a = 32; //100000
//๋ค์์ 4๋ฒ์งธ index์ ์์๋ฅผ ์ถ๊ฐํ๊ณ ์ ํ ๋, ๋ง๋ค๋ ค๋ ์๋ 101000
System.out.println(Integer.toBinaryString(1 << 3));
System.out.println(a | (1 << 3)); //40
}
}
/**
* ์์ ์ญ์
* - current ์์ p๋ฒ ์์๋ง ์ญ์
* - p๋ฒ ๋นํธ๋ฅผ 0์ผ๋ก ๋ฐ๊พธ์ด์ฃผ์ด์ผ ํ๋ค.
* */
public class SetRemove {
public static void main(String[] args) {
int p = 40; //101000
//3๋ฒ์งธ ์์๋ฅผ ์ ๊ฑฐํ๋ ค๋ฉด? ๋ชฉํํ๋ ์๋ 100000 = 32
System.out.println(Integer.toBinaryString(1 << 3));
System.out.println(p & ~(1 << 3)); //32
}
}
/**
* 1์ด๋ฉด 0, 0์ด๋ฉด 1๋ก ๋ณํ
* */
public class SetToggle {
public static void main(String[] args) {
int a = 7; //111
//๋ชฉํ : 101 = 5
System.out.println(a ^ 1 << 1);
}
}
/**
* ์งํฉ์ ํฌ๊ธฐ ๊ตฌํ๊ธฐ
* */
public class SetSize {
public static void main(String[] args) {
System.out.println(bitCount(15));
}
private static int bitCount(int num) {
if (num == 0) {
return 0;
}
return num % 2 + bitCount(num / 2);
}
}
/**
* ํฉ์งํฉ ๊ตฌํ๊ธฐ
* */
public class SetCombine {
public static void main(String[] args) {
//a | b : ํฉ์งํฉ
//a & b : ๊ต์งํฉ
//a & ~b : ์ฐจ์งํฉ
//a ^ b : ํฉ์งํฉ - ๊ต์งํฉ = a์ b ์ค ํ๋์๋ง ํฌํจ๋ ์์
}
}
4. ์ฐธ๊ณ
Last updated