valarray の各要素の NOT の値をビットごとに取得単項演算子。
valarray<Type> operator~( ) const;
戻り値
valarray オペランドの要素値のビットごとの NOT であるブール値の valarray。
解説
ビットごとの演算は char のと int のデータ型とバリアントであり [float]、double、[long]double、void、bool またはそのほかの複雑なデータ型のビットを処理する場合にのみ使用できます。
ビットごとの論理 NOT と同じ真理値表はありませんが、個々のビットのレベルのデータ型に適用されます。b 場合 は、b が true と false の場合、特定のビットの ~b bは True です。論理 NOToperator! は trueとしてすべてのゼロ以外の値をカウント要素レベルで適用され結果はブール型の valarray です。ビットごとの **NOToperator~**は、これに対してビットごとの演算の結果によって 0 または 1 以外の値の valarray で、なります。
使用例
// valarray_op_bitnot.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>
int main( )
{
using namespace std;
int i;
valarray<unsigned short int> vaL1 ( 10 );
valarray<unsigned short int> vaNOT1 ( 10 );
for ( i = 0 ; i < 10 ; i += 2 )
vaL1 [ i ] = i;
for ( i = 1 ; i < 10 ; i += 2 )
vaL1 [ i ] = 5*i;
cout << "The initial valarray <unsigned short int> is: ( ";
for ( i = 0 ; i < 10 ; i++ )
cout << vaL1 [ i ] << " ";
cout << ")." << endl;
vaNOT1 = ~vaL1;
cout << "The element-by-element result of "
<< "the bitwise NOT operator~ is the\n valarray: ( ";
for ( i = 0 ; i < 10 ; i++ )
cout << vaNOT1 [ i ] << " ";
cout << ")." << endl << endl;
valarray<int> vaL2 ( 10 );
valarray<int> vaNOT2 ( 10 );
for ( i = 0 ; i < 10 ; i += 2 )
vaL2 [ i ] = i;
for ( i = 1 ; i < 10 ; i += 2 )
vaL2 [ i ] = -2 * i;
cout << "The initial valarray <int> is: ( ";
for ( i = 0 ; i < 10 ; i++ )
cout << vaL2 [ i ] << " ";
cout << ")." << endl;
vaNOT2 = ~vaL2;
cout << "The element-by-element result of "
<< "the bitwise NOT operator~ is the\n valarray: ( ";
for ( i = 0 ; i < 10 ; i++ )
cout << vaNOT2 [ i ] << " ";
cout << ")." << endl;
// The negative numbers are represented using
// the two's complement approach, so adding one
// to the flipped bits returns the negative elements
vaNOT2 = vaNOT2 + 1;
cout << "The element-by-element result of "
<< "adding one\n is the negative of the "
<< "original elements the\n valarray: ( ";
for ( i = 0 ; i < 10 ; i++ )
cout << vaNOT2 [ i ] << " ";
cout << ")." << endl;
}
必要条件
ヘッダー: <valarray>
名前空間: std