logo

C/C++의 왼쪽 Shift 및 오른쪽 Shift 연산자

왼쪽 시프트(<<)

두 개의 숫자를 사용하여 첫 번째 피연산자의 비트를 왼쪽으로 이동하고 두 번째 피연산자는 이동할 자릿수를 결정하는 이항 연산자입니다. 즉, 정수를 왼쪽으로 이동하면 정수로 '로 표시 (a< 곱하는 것과 같습니다 ~와 함께 2^b (2의 거듭제곱 b).

통사론:

a << b;>
    a: 첫 번째 피연산자 b: 두 번째 피연산자

예: 해 보자 a=5 ; 이는 101 바이너리 형태로. 이제 만약에 a는 2만큼 왼쪽으로 이동합니다.a=a<<2 그 다음에 될 것입니다 a=a*(2^2) . 따라서, a=5*(2^2)=20 이는 다음과 같이 쓸 수 있습니다. 10100.

왼쪽 시프트 연산자 기능




// C Program to demonstrate use> // of left shift operator> #include> // Driver code> int> main()> {> >// a = 5(00000101), b = 9(00001001)> >unsigned>char> a = 5, b = 9;> >// The result is 00001010> >printf>(>'a<<1 = %d '>, (a << 1));> >// The result is 00010010> >printf>(>'b<<1 = %d'>, (b << 1));> >return> 0;> }>

>

>

C++




// C++ Program to demonstrate use> // of left shift operator> #include> using> namespace> std;> // Driver code> int> main()> {> >// a = 5(00000101), b = 9(00001001)> >unsigned>char> a = 5, b = 9;> >// The result is 00001010> >cout <<>'a<<1 = '> << (a << 1) << endl;> >// The result is 00010010> >cout <<>'b<<1 = '> << (b << 1) << endl;> >return> 0;> }>

암리타 라오 배우

>

>

산출

a<<1 = 10 b<<1 = 18>

오른쪽 쉬프트(>>)

두 개의 숫자를 사용하여 첫 번째 피연산자의 비트를 오른쪽으로 이동하고 두 번째 피연산자는 이동할 자릿수를 결정하는 이항 연산자입니다. 즉, 정수를 오른쪽으로 이동하면 정수로 '로 표시 (a>>b) '는 a를 2^b로 나누는 것과 같습니다.

통사론:

a>> ㄴ;>
    a: 첫 번째 피연산자 b: 두 번째 피연산자

예: 해 보자 a=5 ; 이는 101 바이너리 형태로. 이제 만약에 a는 2만큼 오른쪽으로 시프트됩니다. 즉, a=a>>2 그 다음에 될 것입니다 a=a/(2^2) . 따라서, a=a/(2^2)=1 이는 다음과 같이 쓸 수 있습니다. 01 .

오른쪽 시프트 연산자 기능




// C Program to demonstrate> // use of right-shift operator> #include> // Driver code> int> main()> {> >// a = 5(00000101), b = 9(00001001)> >unsigned>char> a = 5, b = 9;> >// The result is 00000010> >printf>(>'a>>1 = %d '>, (a>> 1));> >// The result is 00000100> >printf>(>'b>>1 = %d'>, (b>> 1));> >return> 0;> }>

>

>

C++




// C++ Program to demonstrate> // use of right-shift operator> #include> using> namespace> std;> // Driver code> int> main()> {> >// a = 5(00000101), b = 9(00001001)> >unsigned>char> a = 5, b = 9;> >// The result is 00000010> >cout <<>'a>>1 = '> <>1)<< endl;> >// The result is 00000100> >cout <<>'b>>1 = '> <>1)<< endl;> >return> 0;> }>

>

>

산출

a>>1 = 2 비>>1 = 4>

중요사항

1. 음수에는 왼쪽 시프트 및 오른쪽 시프트 연산자를 사용하면 안 됩니다. 피연산자 중 하나라도 음수인 경우 결과는 정의되지 않은 동작입니다. 예를 들어 1>> -1 및 1 << -1의 결과는 모두 정의되지 않습니다.




// C program to show behaviour of shift operators for> // negative values> #include> int> main()> {> >// left shift for negative value> >printf>(>'2 << -5 = %d '>, (2 << -5));> >// right shift for negative value> >printf>(>'2>> -5 = %d'>, (2>> -5));> >return> 0;> }>

>

>

C++




// C++ program to show behaviour of shift operators for> // negative values> #include> using> namespace> std;> int> main()> {> >// left shift for negative value> >cout <<>'2 << -5 = '> << (2 << -5) << endl;> >// right shift for negative value> >cout <<>'2>> -5 = '> <>-5)<< endl;> >return> 0;> }>

>

>

산출

2 <>-5 = 64>

2. 숫자가 정수 크기보다 더 많이 이동하면 동작이 정의되지 않습니다. 예를 들어 정수가 32비트를 사용하여 저장되면 1 << 33은 정의되지 않습니다. 더 큰 값의 비트 이동의 경우 1ULL<<62 큰 값을 저장할 수 있는 64비트를 사용하여 정의된 Unsigned Long Long에 사용됩니다.




// c program to demonstrate the behaviour of bitwise> // shift operators for large values> #include> int> main()> {> >int> N = 3;> >// left shift of 65 digits> >printf>(>'3 << 65 = %d'>, (3 << 65));> >return> 0;> }>

>

>

C++




// c++ program to demonstrate the behaviour of bitwise> // shift operators for large values> #include> using> namespace> std;> int> main()> {> >int> N = 3;> >// left shift by 65 digits> >cout <<>'3 << 65'> << (3 << 65) << endl;> >return> 0;> }>

>

>

산출

3 << 65 = 0>

삼. 왼쪽으로 1만큼 이동하고 오른쪽으로 1만큼 이동하는 것은 각각 첫 번째 항과 2의 거듭제곱을 요소(1<>3 = 1/pow(2,3))에 곱한 것과 동일합니다.




// C program for the above approach> #include> #include> int> main()> {> >printf>(>'2^5 using pow() function: %.0f '>,>pow>(2, 5));> >printf>(>'2^5 using left shift: %d '>, (1 << 5));> >return> 0;> }> // This code is contributed Prince Kumar>

>

>

C++




// C++ program to get the shifted values using pow()> #include> #include> using> namespace> std;> int> main()> {> >cout <<>'2^5 using pow() function'> <<>pow>(2, 5) << endl;> >cout <<>'2^5 using leftshift'> << (1 << 5) << endl;> >return> 0;> }>

>

>

산출

2^5 using pow() function: 32 2^5 using left shift: 32>

읽어야 할 내용: C/C++의 비트 연산자