是(布尔)(I和1)和i%2 == 1一样吗? [英] Are (bool)(i & 1) and i % 2 == 1 same?

查看:318
本文介绍了是(布尔)(I和1)和i%2 == 1一样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(布尔)(I和1)我%2 == 1 总是相同的,其中 I INT

Are (bool)(i & 1) and i % 2 == 1 always same where i is int?

注:总是说我的意思是所有的平台(即使一个字节是16位)和C和C ++所有的标准

Note: saying always I mean for all platforms (even when a byte is 16 bit) and for all standards of C and C++.

编辑:

有关C和C全部达标++,其中布尔存在。

For all standards of C and C++ where bool exist.

推荐答案

没有。

1秒的补重$ P $ INT 的psentation,-1的重presentation是 1 ... 10 ,所以它们之间的区别。

1s' complement representation of int, the representation of -1 is 1 ... 10, so they differ.

总之,我%2 可以是负负 I (事实​​上它要求在C99时,它的不为0),因此不等于1为负奇数。

Anyway, i % 2 can be negative for negative i (indeed it's required to be in C99 when it's not 0), and hence not equal to 1 for negative odd numbers.

这篇关于是(布尔)(I和1)和i%2 == 1一样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆