Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Don’t worry if you are not familiar with the “==” equality operator. It is introduced later in this chapter. Come back to this script after we discuss the equality operator.

Bitwise OR

operand1 | operand2

The OR operator, also known as the inclusive OR operator, compares its operands. It returns 1 if at least one of the compared bits is 1. Table 7-5 shows the operator’s truth table.

Table 7-5. Bitwise OR truth table.


Bit1 Bit2 Bit1 | Bit2

0 0 0
0 1 1
1 0 1
1 1 1

When operating on bytes:

  0x46 010001102
| 0x79 011110012
= 0x7F 011111112

Bitwise XOR

operand1 ^ operand2

The bitwise XOR (shorthand for bitwise exclusive OR) operator returns 1 if only one bit is 1. It results in a 0 if the bits are equal (0 and 0, 1 and 1). Here is the truth table:

Table 7-6. Bitwise XOR truth table.


Bit1 Bit2 Bit1 ^ Bit2

0 0 0
0 1 1
1 0 1
1 1 0

Take a look at the following example:

 0x2C 001011002
^0xA3 101000112
=0x8F 100011112

The bitwise XOR operator is used in a simple cipher technique called XORing, which will be covered later in this book.

Bitwise NOT

The bitwise NOT operator returns the reverse of its operand. Therefore, it is also called the bit flip operator or the invert operator. All 1s are converted to 0s, and all 0s are converted to 1s. Here is the truth table:

Table 7-7. Bitwise NOT.


Bit ~Bit

0 1
1 0

For example,

~70 000000000000000000000000010001102
=  –71  111111111111111111111111101110012

Note that this operator refers to all operands (integers) as 32 bits. If they are not 32-bit integers, they are converted for the operation and then converted back to their initial form.

Shift Operators

The shift operators take two operands:

  • The quantity (value) to be shifted
  • The number of bit positions to shift the first operand

Shift operators convert their operands to 32-bit integers, and return a result of the same type as the left operator. There are three shift operators:

  • Left shift (<<)
  • Right shift (>>)
  • Zero-fill right shift (>>>)

Left shift

operand1 << operand2

The left shift operator shifts the first operand the specified number of bits to the left. All bits that are shifted out (to the left) are discarded. New bits coming in from the right are zeros. In the following example, represents 4 bytes (32 bits):

179=>0xB3=>101100112
179<<0=>00000000000000000000000010110011
179<<2=>   00000000000000000000000010110011
 empty space filled with 0s
=>00000000000000000000001011001100
=>10110011002=>0x2CC=>716

You now know that 179 << 2 is 716. You might realize that 179 * 4 is also 716—this is no coincidence. In general, x << n is the same as x * 2n. Shifting left n places is the same as multiplying by 2n. This rule applies also to negative numbers—the sign is always preserved. Thus, you can choose between two different methods to multiply by a power of two. Although shifting is faster, it is also less clear to the reader and should be avoided.

Right shift

operand1 >> operand2

The right shift operator is also called sign-propagating right shift, because it preserves the sign of the initial operand. Like the left shift operator, the right shift operator shifts the first operand the specified number of bits to the right. Excess bits shifted off the right are discarded. The sign of an integer is stored in the first bit (from the left). In order to preserve the sign, the first bit remains as is. The other 31 bits are shifted to the right. New bits coming in from the left are either 0s or 1s, depending on the sign. If the first bit was a 0, all new bits are 0s, and vice versa. Therefore, if the number was positive, the operation will also return a positive number; if it was negative, the operation evaluates to a negative number. It is important to remember that this operator, like all shift operators, converts the first operand to a 32-bit integer, and after shifting, returns it to its initial representation. Here is a simple example:

176=>0xB0=>101100002
176>>0=>00000000000000000000000010110000
176>>3=>0 00000000000000000000000010110000
=>   00000000000000000000000000010110
=>101102=>0x16=>22

Let’s see how this operator works on a negative number, say, –17.

–17=>111111111111111111111111111011112
–17>>0=> 11111111111111111111111111101111
–17>>3=> 1 1111111111111111111111111101111
=>   11111111111111111111111111111101
=>111111111111111111111111111111012=>–3

You can refer to the right shift operator as the opposite of the left shift one. While the left shift operator is equivalent to multiplying by a power of 2, the right shift operation is equivalent to dividing by a power of 2. 176 >> 3 is equal to 176 / 23. In general, x << n is the same as x / 2n.

Zero-fill right shift

operand1 >>> operand2

The zero-fill right shift operator shifts the first operand the specified number of bits to the right. Like the sign-propagating right shift, the zero-fill right shift discards excess bits that are shifted off to the right. However, the bits shifted in from the left are always zeros. The number’s sign is lost because the leftmost bit is always 0. Here is the simple example from the previous section, this time with the zero-fill right shift:

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us