我在jsfiddle.net上玩,我很好奇为什么这会返回true?
if(0 < 5 < 3) {
alert("True");
}
Run Code Online (Sandbox Code Playgroud)
这样做:
if(0 < 5 < 2) {
alert("True");
}
Run Code Online (Sandbox Code Playgroud)
但这不是:
if(0 < 5 < 1) {
alert("True");
}
Run Code Online (Sandbox Code Playgroud)
这个怪癖是否有用?
许多编译器似乎只保留bool值中的0或1,但我不确定这将始终有效:
int a = 2;
bool b = a;
int c = 3 + b; // 4 or 5?
Run Code Online (Sandbox Code Playgroud) 如果我得到一个bool
变量并将其第二位设置为1,则变量同时评估为true和false。使用带有-g
选项(gcc-v6.3.0/Linux/RHEL6.0-2016-x86_64/bin/g++ -g main.cpp -o mytest_d
)的gcc6.3编译以下代码,然后运行可执行文件。您得到以下内容。
T如何同时等于真和假?
value bits
----- ----
T: 1 0001
after bit change
T: 3 0011
T is true
T is false
Run Code Online (Sandbox Code Playgroud)
当您使用不同的语言(例如fortran)调用函数时,可能会发生这种情况,其中对和错的定义与C ++不同。对于fortran,如果任何位都不为0,则该值为true;如果所有位为零,则该值为false。
#include <iostream>
#include <bitset>
using namespace std;
void set_bits_to_1(void* val){
char *x = static_cast<char *>(val);
for (int i = 0; i<2; i++ ){
*x |= (1UL << i);
}
}
int main(int argc,char *argv[])
{
bool T = 3;
cout <<" value bits " <<endl;
cout <<" …
Run Code Online (Sandbox Code Playgroud) 我知道在C和C++中,当将bool转换为int时,(int)true == 1
和(int)false == 0
.我想知道反向投射......
在下面的代码中,在使用Visual Studio 2013和KeilμVision5编译的.c文件中,以下所有断言都适用于我(bool)2 == true
.
C和C++标准对于将非零,非一个整数转换为bool的说法是什么?是否指定了此行为?请加入引文.
#include <stdbool.h>
#include <assert.h>
void TestBoolCast(void)
{
int i0 = 0, i1 = 1, i2 = 2;
assert((bool)i0 == false);
assert((bool)i1 == true);
assert((bool)i2 == true);
assert(!!i0 == false);
assert(!!i1 == true);
assert(!!i2 == true);
}
Run Code Online (Sandbox Code Playgroud)
不是重复我可以假设(bool)true ==(int)1对于任何C++编译器?:
在这个关于bool和1的问题的评论中帮助解决正在进行的辩论:
符合标准的C++预处理器是否允许#define
用户重新定义语言关键字?如果是这样,符合标准的C++预处理器是否允许这样做?
如果C++程序重新定义了一个语言关键字,那么该程序本身是否符合标准?
考虑下面的C++代码:
bool a = 5;
bool b = 6;
int c = (int)a + (int)b;
Run Code Online (Sandbox Code Playgroud)
当我编译并运行此代码时,c的值为2.标准是否保证在任何编译器/平台中,使用false(0)或true(不一定为1)初始化的bool值在操作中将为1,并且上面的代码将始终为导致c为2?
在C99中,包括stdbool.h,仍然有效吗?
考虑代码
bool f() { return 42; }
if (f() == 1)
printf("hello");
Run Code Online (Sandbox Code Playgroud)
C(带有 stdbool.h 的 C99+)和 C++ 标准是否保证打印“hello”?做
bool a = x;
Run Code Online (Sandbox Code Playgroud)
总是等价于
bool a = x ? 1 : 0;
Run Code Online (Sandbox Code Playgroud) 我正在考虑通过使用uint32_t和bool [4]的并集对返回4个bool的函数进行微优化,然后执行popcnt指令以查看bool数组中有多少个元素是正确的。
But I do not know if the standard guarantees that bool is represented as number with only 1 bit set when true and 0 bits set when it is false.
If the answer is no then I have a follow up question: if it is not required is it required that representation is constant, e.g. if I have a test that checks that true bool casted to uint_8t is 1(and 0 for false) does that this means that …
我读了一些代码并且遇到了这个相当神秘的语法:
size_t count = 1;
char *s = "hello you";
char *last_word = "there";
count += last_word < (s + strlen(s) - 1); #line of interest
Run Code Online (Sandbox Code Playgroud)
不知何故,计数增加了.但我认为<运算符会返回true或false.这条线做什么?
c addition operator-precedence compound-assignment relational-operators
我正在进行演讲,并且关于power point幻灯片说明true将返回0而false将返回1.我声明在Visual Studio中任何非零都被认为是真的,但有没有一个C++标准将0定义为真?
Visual Studio的结果被忽视为非标准但我怀疑它是.
bool tru = true; // returns 0
bool fal = false; // returns 1
Run Code Online (Sandbox Code Playgroud)