对整数;了解一个符号运算符的行为(AMP) [英] Understanding the behavior of a single ampersand operator (&) on integers

查看:145
本文介绍了对整数;了解一个符号运算符的行为(AMP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,单号操作符,通常用于按位与操作。 ?但是,任何人都可以帮助解释有趣的结果,你当你使用它比较两个数字之间

I understand that the single ampersand operator is normally used for a 'bitwise AND' operation. However, can anyone help explain the interesting results you get when you use it for comparison between two numbers?

例如;

(6 & 2) = 2
(10 & 5) = 0
(20 & 25) = 16
(123 & 20) = 16

有似乎是这些结果之间没有任何逻辑联系 - 我错过什么东西?联机文档只似乎是指布尔或单位的比较。

There seems to be no logical link between these results - am I missing something? Online documentation only seems to refer to the comparison of booleans or single bits.

推荐答案

比较各那些的二进制表示。

Compare the binary representations of each of those.

    110 &     010 =     010
   1010 &    0101 =    0000
  10100 &   11001 =   10000
1111011 & 0010100 = 0010000

在每一种情况下,一个数字是在结果1,只有当它为1时在两个左侧和右侧的输入。

In each case, a digit is 1 in the result only when it is 1 on both the left AND right side of the input.

这篇关于对整数;了解一个符号运算符的行为(AMP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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