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