In C, Can two false (zero) be together?

I know that the operator only works if it evaluates to true by any means such as OR/AND

.

I don't understand why computers can't figure this out well, at least in 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 by looking at this code, you can quickly see that 2 is fake and it will be fake.

But what if they both became fake together because the AND operator doesn't make them true? not trying to the operator NOT

here, just don't understand why the 2 false ones checked together and both give the same result, don't they do true?

Now that I've written more about this, it looks like it will break a lot of programs hehe ..

I guess I just thought of a different way of thinking for a second of a second and I didn't understand why it didn't work.

EDIT: yaa I think I am getting a bunch of downvotes. I guess it's just how computers were built, they don't have real logic.

EDIT 2:

Ok I'll give you an example

Say that you need to combine the package of packages so as not to spam too many small packages. But there is a catch, if these small packets are too big, you cannot combine them, just send them as they are. But suppose you have already started combining packets, and there is a large packet in the process, so you cannot proceed further, you must exit and send the current combo packet.

The problem arises when exiting the batch loop in the list.

I am trying to avoid having 2 loops to check the list in expanded state to know what to do, trying to make it as optimized as possible is very important to me.

So I ended up in the 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 did not start combining packages, and the size of the package may fit into the merge package.

But in this case, it doesn't matter if the package has already started combining or has not yet started combining, but it can still be combined, it will not try to combine it.

I think I need to split it up into more if statements.

EDIT 3: 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: finished with

else if(!building_cluster && p->len > MAX_CLUSTER_SIZE)

      

+3


source to share


5 answers


I think you are probably confusing false && false

with double negation. Although double negation becomes affirmative, the way it is expressed in C and C ++ is !false

or !0

.


Your problem description and code are a bit vague, but assuming you really want:

  • if not in the combined package, but the current package is too large, and then send the current
  • if in the combined package, but the current package is too large, then send the combined package, then send the current
  • if not in combo package but current package is small then break off to execute command
  • if in a combined package, but the current package is small, then break off to execute the command


Assuming the above is correct, then you only need to check if your current package is small and will fit, otherwise you need to forward stuff.


Looking at your real code, there seems to be some kind of convoluted logic around whether you are building a cluster now, and whether the current package will fit into your cluster. You will benefit from a simplified structure. The following pseudo code roughly follows what I originally suggested, but keeping in mind how your loop works.

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;
    }
} 

      

+5


source


Boolean and has the following table of values:

  • true && true => true
  • true && false => false
  • false && & true => false
  • false && & false => false


The result is correct only if both operands are correct. This is how it is. itreal 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 have the same value (for example, both true or both false), you can simply use if(a == b) ..

+3


source


The operation &&

does not match the agreement.

The meanings false

here are not about negation.

Values false

here are values.

An operation &&

is a function defined so that true

&&

true

yeilds true

, and any other combination matters 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 is a logical operation and an operation. It is formally defined in Boolean number systems and is usually studied with logical logic.

+1


source


It's 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 the parameters inside yours if

because of &&

. Then it calculates the fisrt value, which is false

. From this point it does not make sense to evaluate the expression - since the first estimated as false

there is no need to assess all future &&

because it is an automatic unit to false: false &&

.

This is why the following is true:

  • true && true => true
  • true && false => false
  • false && & true => false
  • false && & false => false

... which the compiler sees (as at where it stops):

  • true && true => true
  • true && false => false
  • false && & [IT DOESN'T EVEN EVALUATE] => false
  • false && & [IT DOESN'T EVEN EVALUATE] => false
0


source


Check out this "truth table":

http://en.wikipedia.org/wiki/Truth_table#Truth_table_for_all_binary_logical_operators

The operation you are looking for is probably "exclusive NOR", which is "NOT" for "exclusive OR". An exclusive OR is represented by a circle with +

in it in mathematics and, if used for bitwise calculations, a symbol ^

in C. For "logical" calculations, an exclusive OR can be represented by the not equal operator (and exclusive NOR is the equal operator) ...

0


source







All Articles