c#中的&除了逻辑判断之外还代表什么意思?

qq_28273155 qq_28273155 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
leetcode 上的一道算法题,
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.



这道题小白我完全没思路,然后看了一下答案,发现是这么写的:

public class Solution {
public int HammingWeight(uint n) {
int re = 0;

while(0 != n)
{
n = n&(n - 1);
++re;
}

return re;
}
}

那么问题来了,里面关键的n = n&(n - 1);是什么意思?
...全文
评论 7
收藏 已收藏
分享
返回顶部
qq_28273155
C# 111,129 成员 642,540 内容