Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 8 EXPRESSIONS AND OPERATORS

Logical Operators

The bitwise logical operators are often used to set the bit patterns for parameters to methods. Table 8-12 lists the bitwise logical operators.

These operators, except for bitwise negation, are binary and left-associative. The bitwise negation operator is unary.

Table 8-12. The Logical Operators

Operator

Name

Description

&

Bitwise AND

Produces the bitwise AND of the two operands. The resulting bit is 1

 

 

only if both operand bits are 1.

|

Bitwise OR

Produces the bitwise OR of the two operands. The resulting bit is 1 if

 

 

either operand bit is 1.

^

Bitwise XOR

Produces the bitwise XOR of the two operands. The resulting bit is 1

 

 

only if one, but not both, operand bits are 1.

~

Bitwise

Each bit in the operand is switched to its opposite. This produces the

 

negation

one’s complement of the operand.

 

 

 

The binary bitwise operators compare the corresponding bits at each position in each of their two operands, and they set the bit in the return value according to the logical operation.

221

CHAPTER 8 EXPRESSIONS AND OPERATORS

Figure 8-5 shows four examples of the bitwise logical operations.

Figure 8-5. Examples of bitwise logical operators

The following code implements the preceding examples:

const byte x = 12, y = 10; sbyte a;

a = x & y; a = x | y; a = x ^ y; a = ~x;

//a = 8

//a = 14

//a = 6

//a = -13

222

CHAPTER 8 EXPRESSIONS AND OPERATORS

Shift Operators

The bitwise shift operators shift the bit pattern either right or left a specified number of positions, with the vacated bits filled with 0s or 1s. Table 8-13 lists the shift operators.

The shift operators are binary and left-associative. The syntax of the bitwise shift operators is shown here. The number of positions to shift is given by Count.

Operand << Count

// Left shift

 

Operand >> Count

// Right shift

 

Table 8-13. The Shift Operators

 

 

 

 

 

Operator

Name

Description

 

<<

Left shift

Shifts the bit pattern left by the given number of positions. The bits shifted

 

 

 

off the left end are lost. Bit positions opening up on the right are filled

 

 

 

with 0s.

 

>>

Right shift

Shifts the bit pattern right by the given number of positions. Bits shifted off

 

 

 

the right end are lost.

 

 

 

 

 

For the vast majority of programming in C#, you don’t need to know anything about the hardware underneath. If you’re doing bitwise manipulation of signed numbers, however, it can be helpful to know about the numeric representation. The underlying hardware represents signed binary numbers in a form called two’s complement. In two’s-complement representation, positive numbers have their normal binary form. To negate a number, you take the bitwise negation of the number and add 1 to it. This process turns a positive number into its negative representation, and vice versa. In two’s complement, all negative numbers have a 1 in the leftmost bit position. Figure 8-6 shows the negation of the number 12.

Figure 8-6. To get the negation of a two’s-complement number, take its bitwise negation and add 1.

The underlying representation is important when shifting signed numbers because the result of shifting an integral value one bit to the left is the same as multiplying it by two. Shifting it to the right is the same as dividing it by two.

If, however, you were to shift a negative number to the right and the leftmost bit were to be filled with a 0, it would produce the wrong result. The 0 in the leftmost position would indicate a positive number. But this is incorrect, because dividing a negative number by 2 doesn’t produce a positive number.

223

CHAPTER 8 EXPRESSIONS AND OPERATORS

To address this situation, when the operand is a signed integer, if the leftmost bit of the operand is a 1 (indicating a negative number), bit positions opening up on the left are filled with 1s rather than 0s. This maintains the correct two’s-complement representation. For positive or unsigned numbers, bit positions opening up on the left are filled with 0s.

Figure 8-7 shows how the expression 14 << 3 would be evaluated in a byte. This operation causes the following:

Each of the bits in the operand (14) is shifted three places to the left.

The three bit positions vacated on the right end are filled with 0s.

The resulting value is 112.

Figure 8-7. Example of left shift of three bits

Figure 8-8 illustrates bitwise shift operations.

Figure 8-8. Bitwise shifts

The following code implements the preceding examples:

int a, b, x = 14;

a

=

x

<<

3;

//

Shift

left

b

=

x

>>

3;

//

Shift

right

Console.WriteLine("{0} << 3 = {1}" , x, a);

Console.WriteLine("{0} >> 3 = {1}" , x, b);

This code produces the following output:

14 << 3 = 112

14 >> 3 = 1

224

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]