In C, Can two false (zero)'s be true together? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know that a statement only works if the it evaluates to true by any means such as OR/AND.
I don't understand why computers can't understand this well at least C++.
#include <stdio.h>
#include <stdbool.h>
int main(void) {
// your code goes here
if(false && false)
printf("true\n");
else
printf("false\n");
return 0;
}
Output: false
I know that looking at that code you quickly see that 2 falses and it will be false.
But what if they both became false together as a AND operator doesn't that make them true? not trying to the NOT operator here just don't understand why 2 false's checked together and both give the same output don't make a true?
Now that I wrote more about this seems like it would break alot of programs hehe..
I guess I just wondered into a different way of thinking for a split second and I didn't understand why it didn't work that way.
EDIT: yaa i guess i'll be receiving a bunch of downvotes I guess it's just simply how computers were built they don't have real logic
EDIT 2:
Okay I'll give you a example
Say you have to combine a bunch of packets together not to spam too much little packets.
But there is a catch if those small packets are too large you won't be able to combine them at all just send them as they are.
But lets say you already started combining packets and in the process a large packet arrises so you can't possiblely go on any further you have to quit here and send the current combined packet.
The problem arrises when to break out of the loop of packets in the list..
I am trying to avoid having 2 loops to check the list in advanced to know what to do trying to make it as optimized as possible very important to me.
So I got into a program
if(already building a combined packet boolean AND current_packet->size > MAX_COMBINE_PACKET)
break;
else
//don't try to combine the packets just send normally.
So in this situation I haven't started combining packets and the packet size can fit into a combine packet.
But in this case it doesn't matter if the packet already started combining or hasn't started combining yet but still can be combined it will not try to combine it..
I guess I need to split it up into more if statements.
EDIT 3: the real code
/* pull packets from packet list into buf, and erase/free them */
static int pull_packets(packet_list_t *l, uint8_t *buf, bool cluster) {
int bytes_placed = 0;
int offset = 0;
bool building_cluster = false;
int MAX_CLUSTER_SIZE = 0xFF; //maybe make this a constant? 0xFF bytes is the payload max.
while(l->begin() != l->end()) {
PACKET *p = *l->begin();
/* if you are building a cluster and the packet can't be clustered you can't send a regular packet anymore */
/* otherwise just send it as a regular packet, ignore any clustering */
if(building_cluster && p->len > MAX_CLUSTER_SIZE)
break;
else //else if(!building_cluster && p->len > MAX_CLUSTER_SIZE)
cluster = false;
/* if theres room in the packet for cluster+cluster len+[packet] */
if(cluster && p->len <= (MAX_PACKET - offset)) {
if(!building_cluster) {
//starts a new cluster packet
bytes_placed = build_packet( buf, "BBBX", 0x00, 0x0e, p->len, p->data, p->len);
offset += bytes_placed;
building_cluster = true;
free_packet(p);
l->erase(l->begin());
} else {
//appends to existing cluster packet
bytes_placed = build_packet( &buf[offset], "BX", p->len, p->data, p->len);
offset += bytes_placed;
free_packet(p);
l->erase(l->begin());
}
} else {
/* can't create cluster or cluster is filled up */
if(building_cluster) //has a previous cluster in progress
break;
//cluster is filled up
bytes_placed = build_packet(buf, "X", p->data, p->len);
free_packet(p);
l->erase(l->begin());
return bytes_placed;
}
}
return offset;
}
EDIT 4: ended up using
else if(!building_cluster && p->len > MAX_CLUSTER_SIZE)

I think you are probably confusing false && false with double negation. While double negation becomes affirmative, the way this is expressed in C and C++ is !false or !0.
Your problem description and code is a little vague, but assuming what you really want is:
if not in a combined packet but the current packet is too big,then send the current
if in a combined packet but the current packet is too big,then send the combined, then send the current
if not in a combined packet, but the current packet is small,then break to do the combine
if in a combined packet but the current packet is small,then break to do the combine
Assuming the above is correct, then you only have to check to see if your current packet is small and will fit, otherwise you need to forward stuff.
Looking at your real code, there seems to be some confusing logic around whether you are currently building a cluster, and whether the current packet will fit into your cluster. You will benefit from following a simplified structure. The following pseudocode roughly follows what I had originally suggested, but tailored around how your loop actually operates.
bool start = true;
while (packet = head of list of packets) {
if (packet will fit cluster) {
if (start) {
initialize cluster with packet;
start = false;
} else {
add packet to cluster;
}
remove from head;
} else {
if (start) {
initialize cluster with XL packet;
remove from head;
}
break;
}
}

The logical and has the following value table:
true && true => true
true && false => false
false && true => false
false && false => false
The result is only true if both operands are true. That's just the way it is. That is the real logic. If you don't believe me, just read the Wiki article on boolean algebra. It defines 'and' as above.
If you want a condition that evaluates to true if both operands are of the same value (e.g. both true or both false) then you can just use if(a == b) ..

The && operation is not about agreement.
The false values here are not about negation.
The false values here are values.
The && operation is a function, defined such that true && true yeilds true and any other combination yeilds a value false.
In short a && b is shorthand for
a && b = f(a, b)
where
f(a, b) = { false | a = false, b = false
{ false | a = true, b = false
{ false | a = false, b = true
{ true | a = true, b = true
&& is a computer operation that represents a boolean and operation. It is formally defined within boolean number systems, and typically studied with boolean logic.

This is not about that kind of logic. It's about processing logic. In programming languages, when you have something like:
if (false && false) { ... }
The compiler sees options inside your if because of the &&. Then it evaluates the fisrt one, which is false. From that point, there's no point on evaluating the next expression - since the first one evaluates to false, there's no need to evaluate whatever follows &&, because that's an automatic whole false: false &&.
That's why the following is true:
true && true => true
true && false => false
false && true => false
false && false => false
...which, the way the compiler sees it (as in where it stops evaluating) is:
true && true => true
true && false => false
false && [THIS IS NOT EVEN EVALUATED] => false
false && [THIS IS NOT EVEN EVALUATED] => false

Check out this "truth table":
http://en.wikipedia.org/wiki/Truth_table#Truth_table_for_all_binary_logical_operators
The operation you're looking for is probably "exclusive NOR", which is the "NOT" of "exclusive OR". Exclusive OR is represented by a circle with a + in it in mathematics, and, when used for bitwise computations, by the ^ symbol in C-based languages. For "logical" computations exclusive OR can be represented by the "not equals" operator (and "exclusive NOR" is the "equals" operator).

Related

Can Gomega's Equal() handle multiple values ?

I am testing a function to send a mock request to a server using gomega and I want to verify that 1. the request has started 2. the request has completed. For this I am returning two booleans. They should both evaluate to true in the example below but the values will be subject to change. I have tried this:
g := gomega.NewGomegaWithT(t)
...
g.Eventually(func() (bool, bool) {
...
start = false
end = true
if (request.status == "started") {
start = true
}
if (request.status == "complete") {
end = true
}
return start, end
}).Should(Equal((true, true))
But it seems that gomega's Equal() does not handle multiple variables. Is there any way around this? Is it bad practice to evaluate two return values ?
In gomega's doc, It has been said that multiple return is ok to be used.
The function that you pass to Eventually can have more than one return
value. In that case, Eventually passes the first return value to the
matcher and asserts that all other return values are nil or
zero-valued. This allows you to use Eventually with functions that
return a value and an error – a common pattern in Go
In your code, can you change this line
.Should(Equal((true, true)) to .Should(Equal(true, true).
That should fix the problem.
Edit:
I overlooked that Equal only receives one interface parameter. My bad.
For future reference, for comparing multiple values in gomega's eventually,Array of struct (or any datatype) can be useful.
You can use a wrapper function I wrote based on your pseudocode
g := gomega.NewGomegaWithT(t)
...
testedFunc := func() (bool, bool) {
...
start = false
end = true
if (request.status == "started") {
start = true
}
if (request.status == "complete") {
end = true
}
return start, end
}
g.Eventually(func() map[string]bool{
r1,r2 := testedFunc()
return map[string]bool{"start": r1, "end": r2}
}).Should(Equal(map[string]bool{"start": true, "end": true}))
I'm using a map instead of simple r1&r2for verbosity, so, you can see what is actually wrong with the results. In my opinion, it's a bad practice to compare 2 return values unless the second one is an error. You can always combine multiple return values in a single language construct (map, slice, struct, etc.) as I did in my wrapper function. I understand it's hard to do with the async Eventually method, but in general, I'd try to assert each of the return values separately.

how do i make sure a boolean remains true while checking for collision

i am trying to let my character land on a platform in my own SDL2 based framework. currently it is not working due to, if i have more than one platform, which is default if i spawn one. the character will only stay on the last platform added. the rest it will slowly fall through while the grounded boolean switches from true to false.
here is my code:
void NHTVScene::EntitiesGrounded()
{
std::vector<Entity*>::iterator it = platformVector.begin();
while (it != platformVector.end()) {
if (player->isColliding((*it))) {
player->velocity = Vector2(0, 0);
player->grounded = true;
}
else if (!player->isColliding((*it))) {
player->grounded = false;
}
it++;
}
}
this function is called every deltatime update.
i know that this code makes it so that with every platform that is not colliding that the boolean is getting set to true, but i think its weird that it doesn't do that on the newest entry, and i have no clue how to do this properly.
I hope this is enough information to give some insight to my problem. if not, please let me know and i'll supply more.
It is easier to differentiate the while and if else clause with the proper indentation, please consider formatting the nested indentation levels a bit clearer.
I am making an assumption that isColliding(Entity) is a boolean and therefore checking false after already checking true should not necessary. Consider simplifying this logic.
If you want to establish the player as grounded after detecting a collision then that should be your exit criteria.
Something like this should do what you're looking for.
void NHTVScene::EntitiesGrounded()
{
std::vector<Entity*>::iterator it = platformVector.begin();
while (it != platformVector.end())
{
if (player->isColliding((*it)))
{
player->velocity = Vector2(0, 0);
player->grounded = true;
break; // exit criteria met
}
else
{
player->grounded = false;
}
it++;
}
}

Testing - acheving condition coverage with nested if's?

I am trying to find out if it's possible to have a set of test inputs that achieves 100% condition coverage for the following code.
bool a = ...;
bool b = ...;
if (a == True){
if (b == True && a == False){
...
} else{
...
}
} else{
...
}
However, most of the resources I have found only deal with one condition. Therefore I am not sure what to do with nested ifs. Specifically, I am not sure what to do with the second if statement. Since "a == False" should never be true given the outer if statement, is it correct to say that this code can never have 100% condition coverage test cases?
No, it's not possible: (b == True && a == False) will never be true, since it's inside a block
if (a == True)
a can't be true and false at the same time. Either there is a bug, or you have dead code that should simply be removed. And then, you can have 100% coverage.

C++ boolean logic error possibly caused by if statements

Here is an extremely simplified version of a section of code that I am having trouble with.
int i = 0;
int count = 0;
int time = 50;
int steps = 1000;
double Tol = 0.1;
bool crossRes = false;
bool doNext = true;
for (int i=0; i<steps; i++) {
//a lot of operations are done here, I will leave out the details, the only
//important things are that "dif" is calculated each time and doNext either
//stays true or is switched to false
if (doNext = true) {
if (dif <= Tol) count++;
if (count >= time) {
i = steps+1;
crossRes = true;
}
}
}
if (crossRes = true) {
printf("Nothing in this loop should happen if dif is always > Tol
because count should never increment in that case, right?");
}
My issue is that every time it gets done with the for loop, it executes the statements inside the "if (crossRes = true)" brackets even if count is never incremented.
You've made a common (and quite frustrating) mistake:
if (crossRes = true) {
This line assigns crossRes to true and returns true. You're looking to compare crossRes with true, which means you need another equals sign:
if (crossRes == true) {
Or more concisely:
if (crossRes) {
I stand corrected:
if (crossRes)
You wouldn't have this problem if your condition was
if (true = crossRes)
because it wouldn't compile.
`crossRes = true` always evaluates to `true` because it's an assignment, to `true`.
You want `crossRes == true`:
if (crossRes == true) {
printf("Nothing in this loop should happen if dif is always > Tol
because count should never increment in that case, right?");
}
= is assignment, == is equality comparison. You want:
if (crossRes == true) {
You make the same mistake here:
if (doNext = true) { // Bad code
The other answers here have told you the problem. Often your compiler will warn you but a way to ensure that you do not do this is to put the constant term on the left
true == crossRes
that way you get a compiler error instead of a warning and so it can't escape unnoticed since
true = crossRes
wont compile.
First, although a number of people have pointed to the problem with if (crossRes = true), for some reason they haven't (yet, anyway) pointed to the same problem with if (doNext = true).
I'll stick to pointing out that you really want if (crossRes) rather than if (crossRes == true) (or even if (true == crossRes)).
The first reason is that it avoids running into the same problem from a simple typo.
The second is that the result of the comparison is a bool -- so if if (crossRes==true) is necessary, you probably need if (((((crossRes == true) == true) == true) == true) just to be sure (maybe a few more -- you never know). This would, of course, be utterly silly -- you're starting with a bool, so you don't need a comparison to get a bool.
I'd also note for the record, that if you insist on doing a comparison at all, you should almost always use if (x != false) rather than if (x == true). Though it doesn't really apply in C++, in old C that doesn't have an actual Boolean type, any integer type can be used -- but in this case, a comparison to true can give incorrect results. At least normally, false will be 0 and true will be 1 -- but when tested, any non-zero value will count as equivalent to true. For example:
int x = 10;
if (x) // taken
if (x == true) // not taken, but should be.
If you're not starting with a Boolean value as you are here, then the if (<constant> <comparison> <variable>) makes sense and is (IMO) preferred. But when you're starting with a Boolean value anyway, just use it; don't do a comparison to produce another of the same.

Why use if-else if in C++?

Why would you use if-else statements if you can make another if statement?
Example with multiple ifs:
input = getInputFromUser()
if input is "Hello"
greet()
if input is "Bye"
sayGoodbye()
if input is "Hey"
sayHi()
Example with else-if:
input = getInputFromUser()
if input is "Hello"
greet()
else if input is "Bye"
sayGoodbye()
else if input is "Hey"
sayHi()
If you have non-exclusive conditions:
if(a < 100)
{...}
else if (a < 200)
{...}
else if (a < 300)
....
this is very different from the same code without the "else"s...
It's also more performant.
In your first example, every if will be checked, even if input is "Hello". So you have all three checks.
In your second example, execution will stop once it found a branch, so if the user types "Hello" it will be only one check instead of three.
The difference may not be much in your simple example, but imagine that you're executing a potentially expensive function and you might see the difference.
you mean like this:
if (a == true && b == false && c == 1 && d == 0) {
// run if true
}
if (a == false || b == true || c != 1 || d != 0) {
// else
}
An else-statement would be much clearer and easier to maintain.
If you need to chose exactly one action from given set of actions, depending on some conditions, the natural and most clear choice is either switch (don't forget to break after each branch) or combination of if and else. When I write
if (conditon1)
{
action1();
}
else if (condition2)
{
action2();
}
else if (conditon3)
{
action3();
}
.
.
.
else {
action_n();
}
it is clear to the reader that exactly one of actions is to be performed. And there is no possibility that because of mistake in conditions more than one action is performed.
Following your same example if we use sequence of if conditions, whatever the input is it will run all 3 conditions. Replacing sequence of if with if-else conditions will run only first condition in best case whereas all 3 in worst case.
So conclude with that if-else will save our running time in most cases, therefore using if-else is preferred over using sequence of if conditions.
input = getInputFromUser()
if input is "Hello"
greet()
if input is "Bye"
sayGoodbye()
if input is "Hey"
sayHi()