c#中的&除了逻辑判断之外还代表什么意思?
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);是什么意思?
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);是什么意思?
...全文
相关推荐
c#中(&&,||)与(&,|)的区别详解
主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
C#去除字符串中的反斜杠实例(推荐)
下面小编就为大家分享一篇C#去除字符串中的反斜杠实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
C#中的问号(?号)用法小结
主要介绍了C#中的问号(?号)用法小结,本文介绍了3种用法,分别作为修饰符、运算符的用法,需要的朋友可以参考下
单个项目代码,入门逻辑判断必知必会!
单个项目代码,入门逻辑判断必知必会!
C#中的bool类型的学习与应用
bool类型在学习中有着相当重要的地位,好好学习bool可以深化对C#的理解。


