Here are a few questions to help you clarify your understanding of bool and its operations.
When comparing discrete types such as short, long, or char, we can use any of the six comparison operators ( == , != , > , >= , < , and <= ). When comparing floating point types such as double, we cannot effectively use the in/equality comparisons ( == and != ) but can freely use the other four relational operators ( > , >= , < , and <= ). The reason here is that floating point numbers which are not an exact power of 2 are stored imprecisely — with roundoff error — most of the time. Finally, when examining the truth of a logical type (bool), we typically don't use the relational/comparison operators at all. Instead we use the operators for logical 'combination': && , ¦¦ , and ! .
Fill in the truth table for logical and (&&):
X | Y | X && Y |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Fill in the truth table for logical or (¦¦):
X | Y | X ¦¦ Y |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Fill in the truth table for logical not (!):
X | ! X |
---|---|
true | false |
false | true |
Another common logical operation which is not provided by a standard operator is exclusive or (aka XOR — pronounced zor). Here is its truth table:
X | Y | X xor Y |
---|---|---|
true | true | false |
true | false | true |
false | true | true |
false | false | false |
As you can see, it results in true when only one of its operands is true and false otherwise. (Hence its exclusivity...) Show a single bool expression to produce an XOR effect by using &&, ¦¦, and/or ! in combination.
(C) (D) A | B | A ¦¦ B | !(A && B) | C && D -------+-------+----------+-------------+---------- true | true | true | false | false true | false | true | true | true false | true | true | true | true false | false | false | true | false
Given these declarations and initializations:
short x = 4, y = 9; double a = 4.0, b = 10.0; char m = '*', n = '8';
Evaluate the following expressions:
x <= 5 && a == 4 b == 10 ¦¦ n == 8
true && true ???? ¦¦ false true ????
a > 3 && b < 20 m == '*' && n <= '9'
true && true true && true true true
(x+1)%10 == 0 b > y && a < x
5 %10 == 0 true && false 5 == 0 false false
y%2 != 0 b > n ¦¦ y == 9
1 != 0 false ¦¦ true true true
Given these declarations and initializations:
bool x = true, y = false;
Evaluate the following expressions:
x && y x ¦¦ y
false true
x y
true false
!x && y x ¦¦ !y
false true
!x !y
false true
Describe DeMorgan's laws.
These laws explain the properties of AND and OR operations when a logical negation is applied: !(A && B) = !A ¦¦ !B !(A ¦¦ B) = !A && !B
How do these laws apply in programming in general?
When you take the opposite of a logical AND or OR operation, you reduce the number of actions by one: Test | Actions to Evaluate | Normal | Short-Circuit ------------------+------------+---------------- !(x < a ¦¦ x > b) | 4 | 2 x >= a && x <= b | 3 | 1
How do these laws differ from the distribution of negation in numerical algebra?
In numerical algebra, the addends are negated, but the operation totaling them is not altered: -(x + y) = -x + -y -(x - y) = -x - -y
Show code to produce true when a virtual coin toss comes up heads and false when the toss turns up tails.
(bool)(rand() % 2)
Show code to produce true when a generic event which probabilistically occurs does so and false when the event does not happen. Assume that the likelihood of this event happening is given in the variable prob. Be sure to test with the probability not only as 50%, but also as off-center things like 60% or 25%.
rand() % RAND_MAX / (RAND_MAX - 1.0) <= prob
Re-write your coin tossing code to (re)use your generic event happening code.
prob = .5; // repeat <= code above