logo

C의 비트 연산자

비트 연산자는 비트 수준에서 데이터에 대한 연산을 수행하는 데 사용되는 연산자입니다. 비트 단위 연산을 수행하는 것을 비트 수준 프로그래밍이라고도 합니다. 0 또는 1의 두 자리 숫자로 구성됩니다. 주로 수치 계산에서 계산 속도를 높이기 위해 사용됩니다.

C 프로그래밍 언어에는 다양한 유형의 비트 연산자가 있습니다. 다음은 비트 연산자 목록입니다.

운영자 연산자의 의미
& 비트 AND 연산자
| 비트 OR 연산자
^ 비트 배타적 OR 연산자
~ 1의 보수 연산자(단항 연산자)
<< 왼쪽 시프트 연산자
>> 오른쪽 시프트 연산자

비트 연산자의 진리표를 살펴보겠습니다.

엑스 그리고 X&Y X|Y X^Y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 1

비트 AND 연산자

비트 AND 연산자는 단일 앰퍼샌드 기호(&)로 표시됩니다. (&) 연산자의 양쪽에는 두 개의 정수 피연산자가 작성됩니다. 두 피연산자의 해당 비트가 1이면 비트별 AND 연산의 출력은 1입니다. 그렇지 않으면 출력은 0이 됩니다.

예를 들어,

 We have two variables a and b. a =6; b=4; The binary representation of the above two variables are given below: a = 0110 b = 0100 When we apply the bitwise AND operation in the above two variables, i.e., a&amp;b, the output would be: Result = 0100 

위의 결과에서 알 수 있듯이 두 변수의 비트가 하나씩 비교됩니다. 두 변수의 비트가 모두 1이면 출력은 1이고, 그렇지 않으면 0입니다.

비트 AND 연산자를 프로그램을 통해 이해해 봅시다.

 #include int main() { int a=6, b=14; // variable declarations printf(&apos;The output of the Bitwise AND operator a&amp;b is %d&apos;,a&amp;b); return 0; } 

위의 코드에서는 'a'와 'b'라는 두 개의 변수를 만들었습니다. 'a'와 'b'의 값은 각각 6과 14입니다. 'a'와 'b'의 이진값은 각각 0110과 1110입니다. 이 두 변수 사이에 AND 연산자를 적용하면

a AND b = 0110 && 1110 = 0110

산출

C의 비트 연산자

비트 OR 연산자

비트 OR 연산자는 단일 수직 기호(|)로 표시됩니다. (|) 기호 양쪽에 두 개의 정수 피연산자가 기록됩니다. 피연산자의 비트 값이 1이면 출력은 1이 되고, 그렇지 않으면 0이 됩니다.

예를 들어,

 We consider two variables, a = 23; b = 10; The binary representation of the above two variables would be: a = 0001 0111 b = 0000 1010 When we apply the bitwise OR operator in the above two variables, i.e., a|b , then the output would be: Result = 0001 1111 

위의 결과에서 볼 수 있듯이 두 피연산자의 비트가 하나씩 비교됩니다. 두 비트 중 하나의 값이 1이면 출력은 1이고 그렇지 않으면 0입니다.

비트 OR 연산자를 프로그램을 통해 이해해 봅시다.

 #include int main() int a=23,b=10; // variable declarations printf(&apos;The output of the Bitwise OR operator a 

산출

C의 비트 연산자

비트 배타적 OR 연산자

비트 배타적 OR 연산자는 (^) 기호로 표시됩니다. 배타적 OR 연산자의 양쪽에는 두 개의 피연산자가 작성됩니다. 피연산자의 해당 비트가 1이면 출력은 1이 되고, 그렇지 않으면 0이 됩니다.

예를 들어,

 We consider two variables a and b, a = 12; b = 10; The binary representation of the above two variables would be: a = 0000 1100 b = 0000 1010 When we apply the bitwise exclusive OR operator in the above two variables (a^b), then the result would be: Result = 0000 1110 

위의 결과에서 볼 수 있듯이 두 피연산자의 비트가 하나씩 비교됩니다. 피연산자의 해당 비트 값이 1이면 출력은 1이고 그렇지 않으면 0입니다.

비트별 배타적 OR 연산자를 프로그램을 통해 이해해보자.

 #include int main() { int a=12,b=10; // variable declarations printf(&apos;The output of the Bitwise exclusive OR operator a^b is %d&apos;,a^b); return 0; } 

산출

C의 비트 연산자

비트 보수 연산자

비트 보수 연산자는 보수 연산자라고도 합니다. 물결표(~) 기호로 표시됩니다. 하나의 피연산자 또는 변수만 사용하고 피연산자에 대해 보수 연산을 수행합니다. 임의의 비트에 보수 연산을 적용하면 0은 1이 되고 1은 0이 됩니다.

예를 들어,

 If we have a variable named &apos;a&apos;, a = 8; The binary representation of the above variable is given below: a = 1000 When we apply the bitwise complement operator to the operand, then the output would be: Result = 0111 

위의 결과에서 알 수 있듯이 비트가 1이면 0, 그렇지 않으면 1로 변경됩니다.

보수연산자를 프로그램을 통해 이해해보자.

 #include int main() { int a=8; // variable declarations printf(&apos;The output of the Bitwise complement operator ~a is %d&apos;,~a); return 0; } 

산출

C의 비트 연산자

비트 시프트 연산자

C 프로그래밍에는 두 가지 유형의 비트 시프트 연산자가 있습니다. 비트 시프트 연산자는 비트를 왼쪽이나 오른쪽으로 이동합니다. 따라서 비트 시프트 연산자는 두 가지 범주로 나누어진다고 말할 수 있습니다.

  • 왼쪽 시프트 연산자
  • 오른쪽 시프트 연산자

왼쪽 시프트 연산자

자바의 문자열에서

비트 수를 왼쪽으로 이동하는 연산자입니다.

왼쪽 시프트 연산자의 구문은 다음과 같습니다.

 Operand &lt;&lt; n 

어디,

피연산자는 왼쪽 시프트 연산을 적용하는 정수 표현식입니다.

n은 이동될 비트 수입니다.

왼쪽 시프트 연산자의 경우 'n' 비트가 왼쪽으로 이동됩니다. 왼쪽의 'n' 비트는 팝업되고 오른쪽의 'n' 비트는 0으로 채워집니다.

예를 들어,

 Suppose we have a statement: int a = 5; The binary representation of &apos;a&apos; is given below: a = 0101 If we want to left-shift the above representation by 2, then the statement would be: a &lt;&lt; 2; 0101&lt;<2 = 00010100 < pre> <p> <strong>Let&apos;s understand through a program.</strong> </p> <pre> #include int main() { int a=5; // variable initialization printf(&apos;The value of a&lt;<2 is : %d ', a<<2); return 0; } < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/51/bitwise-operator-c-5.webp" alt="Bitwise Operator in C"> <p> <strong>Right-shift operator</strong> </p> <p>It is an operator that shifts the number of bits to the right side.</p> <p> <strong>Syntax of the right-shift operator is given below:</strong> </p> <pre> Operand &gt;&gt; n; </pre> <p> <strong>Where,</strong> </p> <p>Operand is an integer expression on which we apply the right-shift operation.</p> <p>N is the number of bits to be shifted.</p> <p>In the case of the right-shift operator, &apos;n&apos; bits will be shifted on the right-side. The &apos;n&apos; bits on the right-side will be popped out, and &apos;n&apos; bits on the left-side are filled with 0.</p> <p> <strong>For example, </strong> </p> <pre> Suppose we have a statement, int a = 7; The binary representation of the above variable would be: a = 0111 If we want to right-shift the above representation by 2, then the statement would be: a&gt;&gt;2; 0000 0111 &gt;&gt; 2 = 0000 0001 </pre> <p> <strong>Let&apos;s understand through a program.</strong> </p> <pre> #include int main() { int a=7; // variable initialization printf(&apos;The value of a&gt;&gt;2 is : %d &apos;, a&gt;&gt;2); return 0; } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/51/bitwise-operator-c-6.webp" alt="Bitwise Operator in C"> <hr></2></pre></2>

어디,

피연산자는 오른쪽 시프트 연산을 적용하는 정수 표현식입니다.

N은 이동될 비트 수입니다.

오른쪽 시프트 연산자의 경우 'n' 비트가 오른쪽으로 이동됩니다. 오른쪽의 'n' 비트는 튀어나오고, 왼쪽의 'n' 비트는 0으로 채워집니다.

예를 들어,

 Suppose we have a statement, int a = 7; The binary representation of the above variable would be: a = 0111 If we want to right-shift the above representation by 2, then the statement would be: a&gt;&gt;2; 0000 0111 &gt;&gt; 2 = 0000 0001 

프로그램을 통해 이해해보자.

 #include int main() { int a=7; // variable initialization printf(&apos;The value of a&gt;&gt;2 is : %d &apos;, a&gt;&gt;2); return 0; } 

산출

C의 비트 연산자