c++ - 在 C 中,两个假(零)可以一起为真吗?

标签 c++ boolean-logic

关闭。这个问题需要details or clarity .它目前不接受答案。












想改善这个问题吗?通过 editing this post 添加详细信息并澄清问题.

7年前关闭。




Improve this question




我知道一个语句只有在它以任何方式评估为真时才有效,例如 OR/AND .

我不明白为什么计算机至少不能很好地理解 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



我知道看那个代码你很快就会看到 2 个错误,它会是错误的。

但是,如果它们作为 AND 运算符一起变为 false 会怎样?不试图NOT这里的运算符(operator)只是不明白为什么将 2 个 false 一起检查并且两者都给出相同的输出而不是 true?

现在我写了更多关于这个似乎会破坏很多程序呵呵..

我想我只是一瞬间想换一种不同的思维方式,我不明白为什么它不能那样工作。

编辑:是的,我想我会收到一堆反对票,我想这只是计算机的构建方式,它们没有真正的逻辑

编辑2:

好的,我给你举个例子

假设您必须将一堆数据包组合在一起,以免向太多小数据包发送垃圾邮件。
但是有一个问题,如果这些小数据包太大,您将根本无法将它们组合起来,只需按原样发送即可。
但是假设您已经开始组合数据包,并且在此过程中出现了一个大数据包,因此您不可能再继续下去,您必须在此处退出并发送当前的组合数据包。

问题出现在何时打破列表中的数据包循环..

我试图避免有 2 个循环来提前检查列表以了解如何尝试使其尽可能优化对我来说非常重要。

所以我进入了一个程序
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.

所以在这种情况下,我还没有开始合并数据包,数据包大小可以适应合并数据包。

但是在这种情况下,数据包是否已经开始组合或尚未开始组合但仍然可以组合并不重要,它不会尝试组合它..

我想我需要把它分成更多的 if 语句。

编辑 3:真正的代码
/* 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;
}

编辑 4:最终使用
else if(!building_cluster && p->len > MAX_CLUSTER_SIZE)

最佳答案

我想你可能很困惑false && false双重否定。虽然双重否定变为肯定,但在 C 和 C++ 中表达的方式是 !false!0 .

您的问题描述和代码有点模糊,但假设您真正想要的是:

  • 如果不在组合包中但当前包太大,则发送当前
  • 如果在组合包中但当前包太大,则发送组合包,然后发送当前
  • 如果不在组合包中,但当前包很小,则中断进行组合
  • 如果在组合包中但当前包很小,则中断进行组合

  • 假设以上是正确的,那么你只需要检查你当前的数据包是否小并且适合,否则你需要转发东西。

    查看您的真实代码,您当前是否正在构建集群以及当前数据包是否适合您的集群似乎存在一些令人困惑的逻辑。您将从遵循简化的结构中受益。以下伪代码大致遵循我最初的建议,但根据循环的实际操作方式进行了定制。
    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;
        }
    } 
    

    关于c++ - 在 C 中,两个假(零)可以一起为真吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25213921/

    相关文章:

    c++ - g++ 编译时出现 undefined reference 错误

    c++ - 为什么某些情况需要使用 'bitwise' 运算符而不是 'logical'/'equality' 运算符?

    boolean - 使用卡诺图代替真值表有哪些优点/缺点

    xml - 在 XPath 中选择相反的条件?

    mysql - 有人可以告诉我为什么这个查询不起作用吗?

    c++ - 有趣的 if C 中的条件

    c++ - C++中的简单交互式提示

    c++ - STL 容器中的搜索元素

    c++ - 以下代码之间有什么区别,为什么一个有效而另一个无效?

    C++11 原子 x86 内存排序