11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4: Performing Logical Operations 57married). You are either male or female (at least that’s what my driver’slicense 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 performancepenalty 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 lotof properties into a single word is a big advantage.The single bit operatorsThe bitwise operators — AND (&), OR (|) and NOT (~) — perform logic operationson single bits. If you consider 0 to be false and 1 to be true (it doesn’thave to be this way, but it’s a common convention), you can say things likethe 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-3Truth Table for the AND OperatorAND 1 01 1 00 0 0You read this table as the column corresponding to the value of one of thearguments while the row corresponds to the other. Thus, 1 & 0 is 0. (Column 1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!