&的含义在C ++中 [英] meaning of & in C++

查看:37
本文介绍了&的含义在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在某些C ++代码中找到的表达式,我有些困惑:

I am bit confused about a expression I found in some C++ code:

if (m & 1)
pmm=-pmm;

我不是C/C ++编码器,因此Google给了我两件事:

I am not a C/C++ coder, so Google gives me two things:

  1. &是按位AND
  2. if 语法为 if(condition)语句

那么,以上语句如何工作?我是否应该不需要 if((m& 1)== 0)?

So, how does the above statement work? Should I not require a if ((m & 1)==0)?

推荐答案

只需添加更多的技术说明,查看 if(m& 1)的一种简单方法就是测试是否 m odd (即不是2的整数倍):

Just to add to the more technical explanations, a simpler way of viewing if (m & 1) is that is tests whether m is odd (i.e. not an exact multiple of 2):

if (m & 1)
    // m is odd (1, 3, 5, 7, 9, ...) - do something
else
    // m is even (0, 2, 4, 6, 8, ...) - do something else

这篇关于&的含义在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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