Hide minor edits - Show changes to markup

July 16, 2007, at 02:29 PM
by

- July 16, 2007, at 02:26 PM
by

- 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

- 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

- Changed lines 1-25 from:

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.

to:

April 18, 2007, at 04:07 AM
by

- April 18, 2007, at 03:54 AM
by

- 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

- Added lines 1-47:

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.

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.