Swimmer

Bitwise Operation, Operator (비트연산) 본문

개념공부/C, C++

Bitwise Operation, Operator (비트연산)

Zach Choi 2023. 1. 17. 20:51

Reference : https://en.wikipedia.org/wiki/Bitwise_operation

 

Bitwise operation is fast and simple action. because it is directly supported by the processor.

On low-cost processors, bitwise operations are substantially faster than division, several times faster than multiplication, somtimes significantly faster than addition. (really??)

 

  • Bitwise Operator

NOT(bitwise complement)

NOT 0111 -> 1000, NOT10101011 -> 01010100

 

AND ( & )

AND 0101 - 0011 -> 0001, AND0011 - 0010 -> 0010

 

OR ( | )

OR 0101 - 0011 -> 0111, OR 0010 - 1000 -> 1010

 

NOT ( ~ )

Not 0101 -> 1010

 

XOR ( ^ )

XOR 0010 - 1010 -> 1000, XOR 0101 - 0011 -> 0110

 

XOR 비트 연산의 특징

코딩 문제 중 XOR 비트 연산 관련 문제가 있었는데, 인상 깊은 특징을 발견해 정리 문제 내용은 XOR 비트 연산이 이루어진 Encoding 행렬과 원 행렬의 첫번째 인덱스 값을 입력받았을 때, 원 행렬을

iridescentboy.tistory.com

  • Bit shifts

In these operations, the digits are moved (shifted) to the left or right.

 

 

In LeetCode, There is problem that bit operator solve problem easily and faster.

class Solution {
public:
    bool isPowerOfTwo(int n) {

    if(n < 0)
    {
        return false;
    }
    else
    {
        return n && !(n & n-1);
    }
        
    }
};

'개념공부 > C, C++' 카테고리의 다른 글

2차원 Vector Sort 방법  (0) 2023.08.10
[C언어] 변수의 유효 범위 (variable scope)  (0) 2023.07.03
[C++ STL] unordered_map  (0) 2023.01.16
[C++ STL] map  (0) 2023.01.16
[LeetCode] 1. Two Sum  (1) 2023.01.09
Comments