设为首页 加入收藏

TOP

5.3.1 Using bitset Objects or Integral Values
2013-10-07 15:22:43 来源: 作者: 【 】 浏览:61
Tags:5.3.1 Using bitset Objects Integral Values

We said that the bitset class was easier to use than the lower-level bitwise operations on integral values. Let’s look at a simple example and show how we might solve a problem using either a bitset or the bitwise operators. Assume that a teacher has 30 students in a class. Each week the class is given a pass/fail quiz. We’ll track the results of each quiz using one bit per student to represent the pass or fail grade on a given test. We might represent each quiz in either a bitset or as an integral value:

  1. bitset<30> bitset_quiz1; // bitset solution  
  2. unsigned long int_quiz1 = 0; // simulated collection of bits 

In the bitset case we can define bitset_quiz1 to be exactly the size we need. By default each of the bits is set to zero. In the case where we use a built-in type to hold our quiz results, we define int_quiz1 as an unsigned long, meaning that it will have at least 32 bits on any machine. Finally, we explicitly initialize int_quiz1 to ensure that the bits start out with well-defined values.

The teacher must be able to set and test individual bits. For example, assuming that the student represented by position 27 passed, we’d like to be able to set that bit appropriately:

  1. bitset_quiz1.set(27); // indicate student number 27 passed  
  2. int_quiz1 |= 1UL<<27; // indicate student number 27 passed 

In the bitset case we do so directly by passing the bit we want turned on to set. The unsigned long case will take a bit more explanation. The way we’ll set a specific bit is to OR our quiz data with another integer that has only one bit—the one we want—turned on. That is, we need an unsigned long where bit 27 is a one and all the other bits are zero. We can obtain such a value by using the left shift operator and the integer constant 1:

  1. 1UL << 27; // generate a value with only bit number 27 set 

Nowwhenwe bitwise OR this valuewith int_quiz1, all the bits except bit 27 will remain unchanged. That bit will be turned on. We use a compound assignment (Section 1.4.1, p. 13) to OR this value into int_quiz1. This operator, |=, executes in the same way that += does. It is equivalent to the more verbose:

  1. // following assignment is equivalent to int_quiz1 |= 1UL << 27;  
  2. int_quiz1 = int_quiz1 | 1UL << 27; 

Imagine that the teacher reexamined the quiz and discovered that student 27 actually had failed the test. The teacher must now turn off bit 27:

  1. bitset_quiz1.reset(27); // student number 27 failed  
  2. int_quiz1 &= ~(1UL<<27); // student number 27 failed 

Again, the bitset version is direct. We reset the indicated bit. For the simulated case,we need to do the inverse ofwhatwe did to set the bit: This timewe’ll need an integer that has bit 27 turned off and all the other bits turned on. We’ll  bitwise AND this value with our quiz data to turn off just that bit. We can obtain a value with all but bit 27 turned on by inverting our previous value. Applying the bitwise NOT to the previous integer will turn on every bit except the 27th. When we bitwise AND this value with int_quiz1, all except bit 27 will remain unchanged.

Finally, we might want to know how the student at position 27 fared. To do so, we could write

bool status;
status = bitset_quiz1[27]; // how did student number 27 do
status = int_quiz1 & (1UL<<27); // how did student number 27 do

In the bitset case we can fetch the value directly to determine how that student did. In the unsigned long case, the first step is to set the 27th bit of an integer to 1. The bitwise AND of this value with int_quiz1 eva luates to nonzero if bit 27
of int_quiz1 is also on; otherwise, it eva luates to zero.

In general, the library bitset operations are more direct, easier to read, easier to write, and more likely to be used correctly. Moreover, the size of a bitset is not limited by the number of bits in an unsigned. Ordinarily bitset should be used in preference to lower-level direct bit manipulation of integral values.

EXERCISES SECTION 5.3.1

Exercise 5.9: Assume the following two definitions:

unsigned long ul1 = 3, ul2 = 7;

What is the result of each of the following expressions

(a) ul1 & ul2 (c) ul1 | ul2

(b) ul1 && ul2 (d) ul1 || ul2

Exercise 5.10: Rewrite the bitset expressions that set and reset the quiz results using a subscript operator.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇5.3.2 Using the Shift Operators.. 下一篇2.3.7 Define Variables Where Th..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: