Reference.BitwiseXorNot History

Hide minor edits - Show changes to markup

June 02, 2010, at 03:25 PM by Equipo Traduccion -
June 02, 2010, at 03:24 PM by Equipo Traduccion -
Changed lines 1-7 from:

Bitwise NOT (~)

The bitwise NOT operator in C++ is the tilde character ~. Unlike & and |, the bitwise NOT operator is applied to a single operand to its right. Bitwise NOT changes each bit to its opposite: 0 becomes 1, and 1 becomes 0. For example:

    0  1    operand1
to:

Operador NOT (~) a nivel de bits

El operador bit a bit NOT, en C++, es el caracter ~. A diferencia de los operadores AND (&) y OR (|), el operador NOT es aplicado únicamente a un operando. Este operador lo que hace es cambiar cada bit de la palabra por su contrario: los 0 pasan a ser 1, y viceversa. Por ejemplo:

[@

    0  1    operando1   
Changed lines 8-23 from:
    1  0   ~ operand1

    int a = 103;    // binary:  0000000001100111
    int b = ~a;     // binary:  1111111110011000 = -104

You might be surprised to see a negative number like -104 as the result of this operation. This is because the highest bit in an int variable is the so-called sign bit. If the highest bit is 1, the number is interpreted as negative. This encoding of positive and negative numbers is referred to as two's complement. For more information, see the Wikipedia article on two's complement.

As an aside, it is interesting to note that for any integer x, ~x is the same as -x-1.

At times, the sign bit in a signed integer expression can cause some unwanted surprises.

to:
    1  0   ~ operando1

    int a = 103;    // en binario:  0000000001100111
    int b = ~a;     // en binario:  1111111110011000 = -104

@]

Posiblemente te sorprendas de ver que el resultado da un valor negativo. Esto es debido a que el bit más significativo (msb) es llamado, también, bit de signo. Si este bit es un 0, el valor del número será interpretado como positivo, mientras que si es 1, se interpretará como un negativo. Esta codificación para los números positivos y negativos se denomina "'Complemento a 2'" (Ca2). Para más información, lee el artículo correspondiente en la Wikipedia en complemento a dos.

Como anotación, es interesante saber que para cualquier entero X, ~X es lo mismo que X-1

A veces, el bit de signo en un entero con signo (signed int) puede causar algunos problemas imprevistos.

July 16, 2007, at 02:29 PM by Paul Badger -
July 16, 2007, at 02:26 PM by Paul Badger -
Changed lines 16-17 from:

You might be surprised to see a negative number like -104 as the result of this operation. This is because the highest bit in an int variable is the so-called sign bit. If the highest bit is 1, the number is interpreted as negative. This encoding of positive and negative numbers is referred to as two's complement. For more information, see the Wikipedia article on two's complement.

to:

You might be surprised to see a negative number like -104 as the result of this operation. This is because the highest bit in an int variable is the so-called sign bit. If the highest bit is 1, the number is interpreted as negative. This encoding of positive and negative numbers is referred to as two's complement. For more information, see the Wikipedia article on two's complement.

July 16, 2007, at 02:23 PM by Paul Badger -
Changed lines 20-24 from:

At times, the sign bit in a signed integer expression can cause some unwanted surprises.

to:

At times, the sign bit in a signed integer expression can cause some unwanted surprises.

April 18, 2007, at 03:33 PM by Paul Badger -
Changed lines 1-25 from:

Bitwise XOR (^) & Bitwise Not (~)

Bitwise XOR (^)

There is a somewhat unusual operator in C++ called bitwise exclusive OR, also known as bitwise XOR. (In English this is usually pronounced "eks-or".) The bitwise XOR operator is written using the caret symbol ^. This operator is very similar to the bitwise OR operator |, only it evaluates to 0 for a given bit position when both of the input bits for that position are 1:

    1  1  0  0    operand1
    0  1  0  1    operand2
    ----------
    1  0  0  1    (operand1 ^ operand2) - returned result

Another way to look at bitwise XOR is that each bit in the result is a 1 if the input bits are different, or 0 if they are the same.

Here is a simple code example:

    int x = 12;     // binary: 1100
    int y = 10;     // binary: 1010
    int z = x ^ y;  // binary: 0110, or decimal 6

The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0) some of the bits in an integer expression. In a bitwise NOR operation if there is 1 in the mask bit, that bit is inverted, if there is a 0, the bit is not inverted and stays the same. For example:

    y = x ^ B00000001;   // toggle the lowest bit in x, and store the result in y.

Bitwise NOT (~)

to:

Bitwise NOT (~)

April 18, 2007, at 04:07 AM by Paul Badger -
April 18, 2007, at 03:54 AM by Paul Badger -
Changed lines 20-23 from:

The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0) some of the bits in an integer expression while leaving others alone. For example:

    y = x ^ 1;   // toggle the lowest bit in x, and store the result in y.
to:

The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0) some of the bits in an integer expression. In a bitwise NOR operation if there is 1 in the mask bit, that bit is inverted, if there is a 0, the bit is not inverted and stays the same. For example:

    y = x ^ B00000001;   // toggle the lowest bit in x, and store the result in y.
April 18, 2007, at 03:51 AM by Paul Badger -
Added lines 1-47:

Bitwise XOR (^) & Bitwise Not (~)

Bitwise XOR (^)

There is a somewhat unusual operator in C++ called bitwise exclusive OR, also known as bitwise XOR. (In English this is usually pronounced "eks-or".) The bitwise XOR operator is written using the caret symbol ^. This operator is very similar to the bitwise OR operator |, only it evaluates to 0 for a given bit position when both of the input bits for that position are 1:

    1  1  0  0    operand1
    0  1  0  1    operand2
    ----------
    1  0  0  1    (operand1 ^ operand2) - returned result

Another way to look at bitwise XOR is that each bit in the result is a 1 if the input bits are different, or 0 if they are the same.

Here is a simple code example:

    int x = 12;     // binary: 1100
    int y = 10;     // binary: 1010
    int z = x ^ y;  // binary: 0110, or decimal 6

The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0) some of the bits in an integer expression while leaving others alone. For example:

    y = x ^ 1;   // toggle the lowest bit in x, and store the result in y.

Bitwise NOT (~)

The bitwise NOT operator in C++ is the tilde character ~. Unlike & and |, the bitwise NOT operator is applied to a single operand to its right. Bitwise NOT changes each bit to its opposite: 0 becomes 1, and 1 becomes 0. For example:

    0  1    operand1

   ----------
    1  0   ~ operand1

    int a = 103;    // binary:  0000000001100111
    int b = ~a;     // binary:  1111111110011000 = -104

You might be surprised to see a negative number like -104 as the result of this operation. This is because the highest bit in an int variable is the so-called sign bit. If the highest bit is 1, the number is interpreted as negative. This encoding of positive and negative numbers is referred to as two's complement. For more information, see the Wikipedia article on two's complement.

As an aside, it is interesting to note that for any integer x, ~x is the same as -x-1.

At times, the sign bit in a signed integer expression can cause some unwanted surprises.

Share