Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

56

Part I: Introduction to C++ Programming

Roman numeral expressions

On a historical note, I should mention that some numbering systems actually hinder computa­ tions. The Roman numeral system is a (so to speak) classic example that greatly hindered the development of math.

Adding two Roman numerals isn’t too difficult:

XIX + XXVI = XLV

Think this one out:

a) IX + VI: The I after the V cancels out the I before the X so the result is V carry the X.

b) X + XX: Plus the carry X is XXXX, which is expressed as XL.

Subtraction is only slightly more difficult.

Ah, but multiplying two Roman numerals all but requires a bachelor’s degree in mathematics. (You end up with rules like X promotes the digits on the right by 1 letter so that X –* IV becomes XL.) Division practically required a Ph.D., and higher operations such as integration would have been completely impossible.

Love those Arabic numerals . . .

Performing Bitwise Logical Operations

All C++ numbers can be expressed in binary form. Binary numbers use only the digits 1 and 0 to represent a value. The following Table 4-2 defines the set of operations that work on numbers one bit at a time, hence the term bitwise operators.

Table 4-2

Bitwise Operators

Operator

Function

~

NOT: Toggle each bit from 1 to 0 and from 0 to 1

 

 

&

AND each bit of the left-hand argument with that on the right

 

 

|

OR each bit of the left-hand argument with that on the right

 

 

^

XOR (exclusive OR) each bit of the left-hand argument with that on

 

the right

Bitwise operations can potentially store a lot of information in a small amount of memory. There are a lot of traits in the world that have only two (or, at most, four) possibilities — that are either this way or that way. You are either married or you’re not (you might be divorced but you are still not currently

Chapter 4: Performing Logical Operations

57

married). You are either male or female (at least that’s what my driver’s license says). In C++, you can store each of these traits in a single bit — in this way, you can pack 32 separate properties into a single 32-bit int.

In addition, bit operations can be extremely fast. There is no performance penalty paid for that 32-to-1 saving.

Even though memory is cheap these days, it’s not unlimited. Sometimes, when you’re storing large amounts of data, this ability to pack a whole lot of properties into a single word is a big advantage.

The single bit operators

The bitwise operators — AND (&), OR (|) and NOT (~) — perform logic oper­ ations on single bits. If you consider 0 to be false and 1 to be true (it doesn’t have to be this way, but it’s a common convention), you can say things like the following for the NOT operator:

NOT 1 (true) is 0 (false)

NOT 0 (false) is 1 (true)

The AND operator is defined as following:

1 (true) AND 1 (true) is 1 (true)

1 (true) AND 0 (false) is 0 (false)

It’s a similar situation for the OR operator:

1 (true) OR 0 (false) is 1 (true)

0 (false) OR 0 (false) is 0 (false)

The definition of the AND operator appears in Table 4-3.

Table 4-3

 

Truth Table for the AND Operator

AND

1

0

1

1

0

 

 

 

0

0

0

 

 

 

You read this table as the column corresponding to the value of one of the arguments while the row corresponds to the other. Thus, 1 & 0 is 0. (Column 1

58

Part I: Introduction to C++ Programming

and row 0.) The only combination that returns anything other than 0 is 1 & 1. (This is known as a truth table.)

Similarly, the truth table for the OR operator is shown in Table 4-4.

Table 4-4

 

Truth Table for the OR Operator

XOR

1

0

1

1

1

 

 

 

0

1

0

 

 

 

One other logical operation that is not so commonly used in day-to-day living is the OR ELSE operator commonly contracted to XOR. XOR is true if either argument is true but not if both are true. The truth table for XOR is shown in Table 4-5.

Table 4-5

 

Truth Table for the XOR Operator

XOR

1

0

1

0

1

 

 

 

0

1

0

 

 

 

Armed with these single bit operators, we can take on the C++ bitwise logical operations.

Using the bitwise operators

The bitwise operators operate on each bit separately.

The bitwise operators are used much like any other binary arithmetic operator. The NOT operator is the easiest to understand. To NOT a number is to NOT each bit that makes up that number (and to a programmer, that sentence makes perfect sense — honest). Consider this example:

~01102 (0x6)

10012 (0x9)

Thus we say that ~0x6 equals 0x9.

The following calculation demonstrates the & operator:

Chapter 4: Performing Logical Operations

59

01102

&

00112

00102

Beginning with the most significant bit, 0 AND 0 is 0. In the next bit, 1 AND 0 is 0. In bit 3, 1 AND 1 is 1. In the least significant bit, 0 AND 1 is 0.

The same calculation can be performed in hexadecimal by first converting the number in binary, performing the operation and then converting the result back.

0x6

01102

&

&

0x3

00112

0x2

00102

In shorthand, we say that 0x6 & 0x3 equals 0x2.

(Try this test: What is the value of 0x6 | 0x3? Get this in 7 seconds, and you can give yourself 7 pats on the back.)

A simple test

The following program illustrates the bitwise operators in action. The pro­ gram initializes two variables and outputs the result of ANDing, ORing, and XORing them.

//BitTest - initialize two variables and output the

//results of applying the ~,& , | and ^

//operations

#include <cstdio> #include <cstdlib> #include <iostream> using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{

//set output format to hexadecimal cout.setf(cout.hex);

//initialize two arguments

int nArg1; nArg1 = 0x1234;

int nArg2; nArg2 = 0x00ff;

//now perform each operation in turn

//first the unary NOT operator