C/C++ の左シフト演算子と右シフト演算子

C/C++ の左シフト演算子と右シフト演算子

左シフト( < <)

これは 2 つの数値を受け取り、最初のオペランドのビットを左シフトし、2 番目のオペランドでシフトする桁数を決定する二項演算子です。つまり、整数を左シフトすると、 ある 整数で b 「」と表記される (a < 乗算と同等です ある 2^b (2 の b 乗)。

構文:

a  < < b; 
    a: 第 1 オペランド b: 第 2 オペランド

例: 持っていきましょう a=5 ;それは 101 バイナリ形式で。さて、もし a は 2 だけ左シフトされます つまり a=a < <2 それから ある となります a=a*(2^2) 。したがって、 a=5*(2^2)=20 次のように書くことができます 10100。

左シフト演算子関数

C




// 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 

右シフト(>>)

これは 2 つの数値を受け取り、最初のオペランドのビットを右シフトし、2 番目のオペランドでシフトする桁数を決定する二項演算子です。つまり、整数を右シフトすると、 ある 整数で b 「」と表記される (a>>b) ' は a を 2^b で割ることと同じです。

構文:

a>> b;> 
    a: 第 1 オペランド b: 第 2 オペランド

例: 取りましょう a=5 ;それは 101 バイナリ形式で。さて、もし a は 2 だけ右シフトされます つまり、a=a>>2 それから ある となります a=a/(2^2) 。したがって、 a=a/(2^2)=1 次のように書くことができます 01

右シフト演算子関数

C




// 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));>> >1 = %d'>> , (b>> 1));>>

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 b>>1 = 4> 

注意事項

1. 左シフト演算子と右シフト演算子は負の数には使用しないでください。いずれかのオペランドが負の数の場合、結果は未定義の動作になります。たとえば、1>> -1 と 1 < < -1 の両方の結果は未定義です。

C




// 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));>>'

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




// 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 

3. 1 による左シフトと 1 による右シフトは、それぞれ最初の項と 2 の要素のべき乗の積と等価です (1 <>3 = 1/pow(2,3))。

C




// 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++ のビット演算子