gpt4 book ai didi

c++ - 错误 : invalid operands to binary expression ('float' and 'float' ) return (x & (1 << 31)) == 0

转载 作者:行者123 更新时间:2023-11-28 04:48:37 25 4
gpt4 key购买 nike

你好,我有这样一段代码:

STATIC bool is_pos_float(float x) {
return (x & (1 << 31)) == 0;
}

但是编译后显示:

error: invalid operands to binary expression ('float' and 'float')
return (x & (1 << 31)) == 0;

有什么问题?

最佳答案

内置operator&的左操作数必须是integral类型,而不是floating_point。改为这样做。

inline bool is_pos_float(float x) {
return x > 0.0f;
}

编辑。假设 OP 真正想要的是以浮点格式进行处理,我认为如果机器是 Little Endian,这将起作用。

bool high_bit_zero(float x) {
constexpr unsigned sz = sizeof(float);
using raw = unsigned char[sz];
raw *c = reinterpret_cast<raw*>(&x);
return !((*c)[sz-1] & (1 << 7));
}

关于c++ - 错误 : invalid operands to binary expression ('float' and 'float' ) return (x & (1 << 31)) == 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48680206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com