Compartir a través de


valarray::operator~

Un operador unario que obtiene los valores bit a bit de NO de cada elemento del valarray.

valarray<Type> operator~( ) const;

Valor devuelto

El valarray de valores booleanos que son NO bit a bit de los valores de los elementos del operando valarray.

Comentarios

Una operación bit a bit sólo se puede utilizar para manipular los bits en char y los tipos de datos y las variantes de int y no en flotante, double, long double, void, bool u otro, más tipos de datos complejos.

NO Bit a bit tiene la misma tabla de true que NO lógico pero se aplica al tipo de datos en el nivel de bits individuales. Bdeterminada de bit, ~b es true si b es false y false si b es true. NO Lógicoel operador! aplica en un nivel de elemento, contando todos los valores distintos de cero como true, y el resultado es un valarray de valores booleanos. NO Bit a bitoperator~, por el contrario, puede producir un valarray de valores distintos de 0 u 1, dependiendo de la operación bit a bit.

Ejemplo

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

Requisitos

Encabezado: <valarray>

Espacio de nombres: std

Vea también

Referencia

valarray (Clase)